【问题标题】:Nginx www. to non-www. redirect not workingNginx 万维网。到非 www。重定向不起作用
【发布时间】:2015-06-09 16:16:34
【问题描述】:

我按照https://stackoverflow.com/a/11733363/2532070 的说明将 www 重定向到非 www。我正在尝试重定向以下格式:

http://example.com
http://www.example.com
https://www.example.com

全部到:

https://example.com

http://example.com 正在重定向到https。但是,另外两个 www. 不是。这是我的 nginx.conf:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}
#
# Redirect all www to non-www
#
server {
    server_name          www.example.com;
    ssl_certificate      /src/bin/ssl/ssl-bundle.crt;
    ssl_certificate_key  /etc/ssl/private/STAR_example_com.key;
    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80 ipv6only=on;
    listen               [::]:443 ssl spdy ipv6only=on;

    return 301 https://example.com$request_uri;
}

#
# Redirect all non-encrypted to encrypted
#
server {
    server_name          example.com;
    listen               *:80;
    listen               [::]:80;

    return 301 https://example.com$request_uri;
}

#
# There we go!
#
server {
    server_name          example.com;
    ssl_certificate      /src/bin/ssl/ssl-bundle.crt;
    ssl_certificate_key  /etc/ssl/private/STAR_example_com.key;
    listen               *:443 ssl spdy;
    listen               [::]:443 ssl spdy;

    # rest goes here...

    root /usr/share/nginx/html;
    index base.html index.html index.htm;

    client_max_body_size 4G;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /src/media;
    expires 1y;
    add_header Cache-Control "public";
    }

    # your Django project's static files - amend as required
    location /static {
        alias /src/static;
    expires 1y;
    add_header Cache-Control "public";
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

我在 nginx 日志文件中没有看到任何指示错误的内容。当我尝试访问www. 版本时,是否有地方可以查找错误?谢谢!

【问题讨论】:

  • 您有 www.example.com 的 DNS 记录吗?

标签: redirect ssl nginx ssl-certificate


【解决方案1】:

您很可能遇到浏览器缓存问题。尝试清除缓存,例如检查 Chrome 开发工具网络选项卡或 Firefox 开发工具设置中的禁用缓存。那应该可以解决它。

【讨论】:

  • 感谢您的帮助!试过了,没有帮助。我仍然收到Failed to load resource: net::ERR_NAME_NOT_RESOLVED 错误(也许这个浏览器错误有帮助?)。
  • 这表示 DNS 问题,请仔细检查您的 nginx 配置中的域名是否有任何拼写错误。
  • 谢谢 - 这是否意味着我需要将 www.example.com 指向我的 IP 地址,就像我拥有 example.com 一样? (也许这是初学者的错误?)
  • 不,您只需确保您的 DNS 将 www.example.com 和 example.com 解析为您服务器的 IP。我猜您自己并没有运行 DNS,因此请与您的域提供商核实他的一切是否正常。您可以使用nslookup example.comdig example.com 在命令行中直接查询DNS 系统。
猜你喜欢
  • 2015-11-15
  • 2015-12-16
  • 2017-01-11
  • 1970-01-01
  • 2021-09-16
  • 2023-04-02
相关资源
最近更新 更多