使用Nginx将非wwwredirect到www

尝试将https://example.comredirect到https://www.example.com时出现错误。

当我转到https://example.com时 ,它不会redirect并返回页面/ 200状态。

我不想要这个,我希望它redirect到https://www.example.com 。

当我访问http://example.com时 ,它会redirect到https://www.example.com

有人可以告诉我我要去哪里吗?

这是我的默认和default-sslconfiguration文件:

default.conf

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

默认的ssl.conf

 upstream app_server_ssl { server unix:/tmp/unicorn.sock fail_timeout=0; } server { server_name example.com; return 301 https://www.example.com$request_uri } server { server_name www.example.com; listen 443; root /home/app/myproject/current/public; index index.html index.htm; error_log /srv/www/example.com/logs/error.log info; access_log /srv/www/example.com/logs/access.log combined; ssl on; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_certificate /srv/www/example.com/keys/ssl.crt; ssl_certificate_key /srv/www/example.com/keys/www.example.com.key; ssl_ciphers AES128-SHA:RC4-MD5:ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:RSA+3DES:!ADH:!AECDH:!MD5:AES128-SHA; ssl_prefer_server_ciphers on; client_max_body_size 20M; try_files $uri/index.html $uri.html $uri @app; # CVE-2013-2028 http://mailman.nginx.org/pipermail/nginx-announce/2013/000112.html if ($http_transfer_encoding ~* chunked) { return 444; } location @app { 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_ssl; } error_page 500 502 503 504 /500.html; location = /500.html { root /home/app/example/current/public; } } 

您在文件default-ssl.conf中缺lesslisten指令。 添加listen 443; 在这个指令中

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

默认情况下,如果你忽略这个指令,nginx假定你想监听端口80. 这里是默认行为的文档 。


编辑:感谢评论@TeroKilkanen。

这里是default-ssl.conf的完整configuration

 server { listen 443 ssl; server_name example.com; ssl_certificate /srv/www/example.com/keys/ssl.crt; ssl_certificate_key /srv/www/example.com/keys/www.example.com.key; return 301 https://www.example.com$request_uri; } 

旁注 :你可以replacessl on; 指令与listen 443 ssl; 作为nginx文档的build议。

我这样做的方式是在redirect到www的https的ssl服务器块中使用if语句

 ssl_certificate /srv/www/example.com/keys/ssl.crt; ssl_certificate_key /srv/www/example.com/keys/www.example.com.key; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers AES128-SHA:RC4-MD5:ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:RSA+3DES:!ADH:!AECDH:!MD5:AES128-SHA; ssl_prefer_server_ciphers on; client_max_body_size 20M; upstream app_server_ssl { server unix:/tmp/unicorn.sock fail_timeout=0; } server { server_name example.com; return 301 https://www.example.com$request_uri } server { listen 443 default_server ssl; server_name www.example.com; # redirect https://example.com to https://www.example.com # mainly for SEO purposes etc #we will use a variable to do that set $redirect_var 0; if ($host = 'example.com') { set $redirect_var 1; } if ($host = 'www.example.com') { set $redirect_var 1; } if ($redirect_var = 1) { return 301 https://www.example.com$request_uri; } try_files $uri/index.html $uri.html $uri @app; # CVE-2013-2028 http://mailman.nginx.org/pipermail/nginx-announce/2013/000112.html if ($http_transfer_encoding ~* chunked) { return 444; } location @app { 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_ssl; } error_page 500 502 503 504 /500.html; location = /500.html { root /home/app/example/current/public; } } 

当然,只要你想在nginxconfiguration文件中使用if语句, 你应该阅读: https : //www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

只要扔一个if语句,你应该在你的路上。 我检查了curl.exe -I的结果,除https://www.example.com之外的所有情况都被视为301.SSL很棘手,因为它在获得301 URLredirect之前被检查。 因此,您会收到证书错误。

就我个人而言,我喜欢从域名中删除www,但我在下面写了我的代码来回答你的问题。

 server { listen 443 ssl; listen [::]:443 ssl; # IPV6 server_name example.com www.example.com; # List all variations here # If the domain is https://example.com, lets fix it! if ($host = 'example.com') { return 301 https://www.example.com$request_uri; } # If the domain is https://www.example.com, it's OK! No changes necessary! ... # SSL .pem stuff ... } server { listen 80; listen [::]:80; # If the domain is http://example.com or https://www.example.com, let's change it to https! server_name example.com www.example.com; return 310 https://www.example.com$request_uri; } 

将所有请求redirect到https://www.example

在SSL端口(通常为443)上创buildredirect和主域的服务器块以及默认的http端口80

 # non-www to ssl www redirect server { listen 80; listen 443 ssl; server_name example.com; return 301 https://www.example.com$request_uri; # ... ssl certs } # ssl setup for www (primary) domain server { listen 80; listen 443 ssl; server_name www.example.com; if ($scheme = http) { return 301 https://www.example.com$request_uri; } # ... the rest of your config + ssl certs } 

保存并按照sudo nginx -s reload

这将redirect

 http://example 301 -> https://www.example https://example 301 -> https://www.example http://www.example 301 -> https://www.example https://www.example 200