Nginx-如何针对Bot http/https将(301)www正确重定向到非www? [英] Nginx - how to redirect (301) www to non-www correctly for bot http /https?

查看:71
本文介绍了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.

这篇关于Nginx-如何针对Bot http/https将(301)www正确重定向到非www?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆