我在/etc/nginx/conf.d/下有2个nginx的conf文件ssl.conf和default.conf 。 第一个文件处理传入的http请求。 它将http请求重写为https。 ssl.conf文件处理基于https的请求。
下面是, default.conf样子
server { listen 80 default_server; server_name abc.example.com 123-abc.example.com; port_in_redirect off; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; set_real_ip_from 127.0.0.1; set_real_ip_from 10.0.0.0/8; real_ip_header X-Forwarded-For; real_ip_recursive on; location /about { root /usr/share/nginx/html; add_header X-Frame-Options "DENY"; try_files $uri /about.html; } location /webapi { add_header X-Frame-Options "DENY"; } location / { rewrite ^ https://$server_name$request_uri? permanent; } }
这里的问题是, abc.example.com/to/some/path to https://abc.example.com/to/some/path abc.example.com/to/some/path重写为https://abc.example.com/to/some/path这是预期的结果。
但
123-abc.example.com/to/some/path也会重写为https://abc.example.com/to/some/path而不是https://123-abc.example.com/to/some/path
我想通过设置2个不同的server_names作为别名有相同的方式有2个URL。 他们不是devise为服务不同的网页/网站。
这是你的重写:
rewrite ^ https://$server_name$request_uri? permanent;
问题是您使用了$server_name ,这不是浏览器请求的HTTP主机名,而是server块中定义的第一个server_name 。
要解决此问题,请将其更改为$http_host ,这是浏览器请求的主机名。
rewrite ^ https://$http_host$request_uri? permanent;
更好的是,根本不要使用rewrite 。 return 301足够了。
return 301 https://$http_host$request_uri$is_args$args;