2017-04-11 95 views
0

我想所有的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://stackoverflow.com/questions/43081780/dns-records-redirect-www-to-non-www/43089681#43089681)可能會有幫助。 –

回答

1

嘗試使用此server塊代碼:

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

希望這會有所幫助。

相關問題