我正在努力获得以下工作,以便无论您在URL栏中input什么内容,都可以访问“ https://www. ”。 到目前为止www。 不会转到https://www. 和https://不会转到https://www.
以下是我的代码。 需要做什么?
server { listen 80; server_name example.com; rewrite ^(.*) https://www.example.com$1 permanent; } server { listen 80; server_name www.example.com; #rewrite ^(.*) https://www.example.com$1 permanent; root /home/knownsrv/public_html; listen 443 ssl; #server_name example.com www.example.com; #root /home/knownsrv/public_html; ssl_certificate /root/knownsrv.crt; ssl_certificate_key /root/knownsrv.key; #ssl_client_certificate /root/chain.cer; #charset koi8-r; #access_log logs/host.access.log main; location / { root /home/knownsrv/public_html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root /home/knownsrv/public_html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }
你太过于复杂了….
要将带有或不带www的HTTPredirect到带有www的HTTPS:
server { listen 80; server_name www.example.com example.com; return 301 https://www.example.com$request_uri; }
只留下没有www的HTTPS的情况,可以这样解决:
server { listen 443 ssl; server_name example.com; return 301 https://www.example.com$request_uri; ssl_certificate /root/knownsrv.crt; ssl_certificate_key /root/knownsrv.key; }
然后只需添加您的ssl vhost为www.example.com 。 其他一切(即缺lesswww和/或非ssl)将被redirect到它。 不需要if (这会严重降低nginx的性能)。
在服务器块中添加:
if ($host !~* ^www\.) { rewrite ^(.*)$ https://www.website.com$1 permanent; }
所以你的configuration应该是这样的:
server { listen 80; server_name website.com www.website.com; rewrite ^(.*) https://www.website.com$1 permanent; } server { listen 443 ssl; server_name website.com www.website.com; root /home/knownsrv/public_html; if ($host !~* ^www\.) { rewrite ^(.*)$ https://www.website.com$1 permanent; } ssl_certificate /root/knownsrv.crt; ssl_certificate_key /root/knownsrv.key; location / { root /home/knownsrv/public_html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root /home/knownsrv/public_html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }