Nginx www.非www.重定向不起作用 [英] Nginx www. to non-www. redirect not working

查看:55
本文介绍了Nginx www.非www.重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I followed the instructions at https://stackoverflow.com/a/11733363/2532070 to redirect www to non-www. I'm trying to redirect the following formats:

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

全部到:

https://example.com

http://example.com重定向到https.但是,其他两个www.不是.这是我的nginx.conf:

http://example.com is redirecting to https. However, the other two, with www., are not. Here is my 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.版本时,有什么地方可以寻找该错误?谢谢!

I'm not seeing anything in the nginx log file to indicate the error. Is there somewhere to look for the error when I try to access a www. version? Thanks!

推荐答案

您最有可能遇到浏览器缓存问题.尝试清除缓存,例如请在Chrome开发者工具的网络"标签或Firefox的开发者工具设置中检查禁用缓存".那应该解决它.

You are most likely experiencing problems with browser caching. Try purging your cache, e.g. check the disable cache in Chrome dev tools network tab or in Firefox's dev tools settings. That should fix it.

这篇关于Nginx www.非www.重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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