WWW非WWW重定向不工作的HTTPS上nginx的

问题描述:

我想所有的URL example.com,而不是www.example.com重定向。它工作正常,当我键入www.example.com和回车其重定向到https://domain.com。但是,当我输入https://www.example.com中它会为https://www.example.com中,但我想它重定向到https://示例.com。我也跟着类似的问题的一些解决方案,但没有奏效。以下是我的nginx配置文件。WWW非WWW重定向不工作的HTTPS上nginx的

upstream puma { 
    server unix:///home/deploy/apps/myprod/shared/tmp/sockets/myprod-puma.sock; 
} 
server { 
    listen 80; 
    listen [::]:80; 
    server_name example.com; 
    return 301 https://$server_name$request_uri; 
} 

server { 
    #listen 80 default_server deferred; 
    #listen 80; 
    listen 443 default ssl; 

    server_name domain.com; 

    ssl_certificate /etc/nginx/ssl/domain.com.chained.crt; 
    ssl_certificate_key /etc/nginx/ssl/domain.key; 


    root /home/deploy/apps/myprod/current/public; 
    access_log /home/deploy/apps/myprod/current/log/nginx.access.log; 
    error_log /home/deploy/apps/myprod/current/log/nginx.error.log info; 

    #location ^~ /assets/ { 
    #gzip_static on; 
    #expires max; 
    #add_header Cache-Control public; 
    #} 

    location ^~ /(assets|fonts|swfs|images)/ { 
    gzip_static on; 
    expires max; 
    add_header Cache-Control public; 
    } 

    try_files $uri/index.html $uri @puma; 
    location @puma { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 

    proxy_pass http://puma; 
    } 
} 

请能有人告诉我应该做哪些改变来实现这种非WWW重定向

+0

[这个答案](http://*.com/questions/43081780/dns-records-redirect-www-to-non-www/43089681#43089681)可能会有帮助。 –

尝试使用此server块代码:

server { 
    listen 80; 
    listen 443; 
    server_name www.example.com; 
    return 301 https://$server_name$request_uri; 
} 

希望这会有所帮助。