技术解析

请教一个 Ngxin 换域名 301 重定向的问题,两个域名都带 ssl
0
2021-05-21 02:50:45
idczone
之前做了一个网站,采用的是个人备案,因为有博客性质和留言功能,涉及到交互,为了避免以后美国服务器出现隐患,打算还是换个域名并且采用备案。

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

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

这个是怎么回事?
贴设置

因为涉及到两个域名以及两个 ssl 证书,采用了比较笨的方法。两个配置文件。
a.com.conf
server {
server_name www.a.com a.com;

配置
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;

/etc/letsencrypt/live/www.b.com/fullchain.pem;
/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 证书配置
/etc/letsencrypt/live/www.a.com/fullchain.pem;
/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;
}
}

#1

也请帮我看看这个问题
不怎么会用 nginx,我想把 xxxx.com 重定向为 xxxx.com/home,在 nginx 尝试了以下配置都没法生效
rewrite ^/ /home permanent;
rewrite ^/ xxxx.com/home permanent;

location / {
rewrite ^/ /home permanent;
}

我的是这样配置的
```
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
以下配置省略
```

更优雅的配置:
```
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;
}
```


```
server = / {
return 301 /home;
}
```

两个 server 中间需要添加 location 那些么?

不用

location = / { return 301 /home; }
这里用「 location /」 做跳转是不行的,要用「 location = /」。

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服