Nginx-如何针对Bot http/https将(301)www正确重定向到非www?
使用以下Nginx配置文件,我目前可以将所有HTTP www请求永久重定向到HTTPS non-www. http://www.example.com => https://example; com
With the following Nginx config file, I currently can redirect permanently all HTTP www request to HTTPS non-www . http://www.example.com => https://example;com
所有HTTPS非www请求都处理得很好.. https://example.com
All HTTPS non-www request ar well handed .. https://example.com
但是,www HTTPS请求不会重定向到非www HTTPS https://www.examples.com -> https://www.examples.com ->
But, www HTTPS request are NOT redirected to non-www HTTPS https://www.examples.com --> https://www.examples.com I'ld like to have : https://www.examples.com --> https://examples.com
我的配置中缺少什么? 感谢您的反馈
what's missing in my config ? thanks for feedback
default.conf
default.conf
server {
server_name example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
您的配置中没有任何内容可以处理将https://www.example.com
重定向到https://example.com
(但是您知道的).
Nothing in your configuration handles redirecting https://www.example.com
to https://example.com
(but you knew that).
假设您的证书对www.example.com
有效,则可以为端口443创建一个单独的server
块,并将其标记为default_server
(例如您已经对端口80拥有).
Assuming that your certificate is valid for www.example.com
, you could create a separate server
block for port 443 and mark it as a default_server
(such as you already have for port 80).
实际上,您可以将不是https://www.example.com
的所有内容组合到单个server
块中:
In fact you could combine everything that isn't https://www.example.com
into a single server
block:
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
...
}
有关详细信息,请参见此文档.
See this document for details.