请教一个 Ngxin 换域名 301 重定向的问题,两个域名都带 ssl

2017-11-29 14:53:34 +08:00
 nonozone
之前做了一个网站,采用的是个人备案,因为有博客性质和留言功能,涉及到交互,为了避免以后出现隐患,打算还是换个域名并且采用备案。

目前两个域名都已经备案,并且可以实现两个域名访问都可以小绿锁了。

现在的问题是,如果直接访问 b.com ,是可以跳转到 a.com 的,但是如果访问 b.com/123.html 就不会跳转到 a.com/123.html,b.com/123.html 可以正常访问。

这个是怎么回事?
2689 次点击
所在节点    NGINX
9 条回复
LokiSharp
2017-11-29 15:17:40 +08:00
贴设置
nonozone
2017-11-29 15:24:12 +08:00
因为涉及到两个域名以及两个 ssl 证书,采用了比较笨的方法。两个配置文件。
a.com.conf

server {
server_name www.a.com a.com;

#301 配置
if ($host != 'www.b.com') {
rewrite ^/(.*)$ https://www.b.com/$1 permanent;
}

#其他的配置参数

}

server {
listen 80;
listen 443 ssl;

# SSL 证书配置
ssl_certificate /etc/letsencrypt/live/www.a.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.a.com/privkey.pem;

#ssl_certificate /etc/letsencrypt/live/www.b.com/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/www.b.com/privkey.pem;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

server_name www.a.com;

root /srv/www/a.com/public_html;
access_log /var/log/www/a.com.access.log;
error_log /var/log/www/a.com.error.log;

location / {
index index.php index.html index.htm;
if (-f $request_filename) {
expires 30d;
break;
}
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location /nginx-status {
stub_status on;
access_log off;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.a.com.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/a.com/public_html$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi_params;
}
}

-----------
b.com.conf


server {
listen 80;
server_name www.b.com b.com www.a.com a.com;
return 301 https://www.b.com$request_uri;

}


server {
listen 80;
listen 443 ssl;

# SSL 证书配置
#ssl_certificate /etc/letsencrypt/live/www.a.com/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/www.a.com/privkey.pem;

ssl_certificate /etc/letsencrypt/live/www.b.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.b.com/privkey.pem;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

server_name www.b.com;

root /srv/www/a.com/public_html;
access_log /var/log/www/a.com.access.log;
error_log /var/log/www/a.com.error.log;

location / {
index index.php index.html index.htm;
if (-f $request_filename) {
expires 30d;
break;
}
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location /nginx-status {
stub_status on;
access_log off;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.a.com.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/a.com/public_html$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi_params;
}
}


@LokiSharp #1
xujiang
2017-11-29 15:27:56 +08:00
也请帮我看看这个问题
不怎么会用 nginx,我想把 xxxx.com 重定向为 xxxx.com/home,在 nginx 尝试了以下配置都没法生效

rewrite ^/ /home permanent;

rewrite ^/ xxxx.com/home permanent;

location / {
rewrite ^/ /home permanent;
}
fhy1994
2017-11-29 15:35:52 +08:00
我的是这样配置的

```
# b.com.conf
# 定义域名
geo $domain {
default "https://a.com";
}
server {
listen 80;
listen 443 ssl;
client_max_body_size 100M;
server_name b.com;
return 301 $domain$request_uri; # 将 b.com 301 重定向到 a.com

以下配置省略
```
gstqc
2017-11-29 15:52:11 +08:00
更优雅的配置:
```
server {
listen 80;
server_name www.a.com a.com b.com;
return 301 https://www.b.com$request_uri;
}

server {
listen 443 ssl;
server_name www.a.com a.com;
return 301 https://www.b.com$request_uri;
}

server {
listen 443 ssl;
server_name b.com;
return 301 https://www.b.com$request_uri;
}
```
gstqc
2017-11-29 15:53:28 +08:00
@xujiang
```
server = / {
return 301 /home;
}
```
nonozone
2017-11-29 16:25:37 +08:00
@gstqc #5 两个 server 中间需要添加 location 那些么?
gstqc
2017-11-29 16:54:00 +08:00
@nonozone 不用
msg7086
2017-11-29 20:58:51 +08:00
location = / { return 301 /home; }

这里用「 location /」 做跳转是不行的,要用「 location = /」。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/410513

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX