在Serverpilot上configuration301redirect强制https:// no-www的最佳方式(nginx / apache服务器)

现在的情况:

我成功地设法在我的Serverpilot(nginx / apache)服务器上手动设置SSL证书。 因为我的网站在Serverpilot服务器上运行,所以部分是nginx,部分是Apache。 我可以在我的服务器上执行Apache中的所有redirect,但我更喜欢在我的nginxconfiguration文件中以正确的方式执行,因为我已经读过这种方法。

由于nginx由serverpilotpipe理,所以我必须在位于我的服务器上的这个文件夹中的一个单独的nginxconfiguration文件中进行更改:/etc/nginx-sp/vhosts.d。 如您所见,Serverpilot不使用标准nginxconfiguration,Serverpilot安装来自Serverpilotconfiguration文件。

这个/etc/nginx-sp/vhosts.d文件夹中有两个文件:

  1. example.conf(这是Serverpilot的标准configuration文件,我没有改变它)
  2. example.ssl.conf(这是我用ssl规则创build的文件)

据我所知,Nginx首先读取第一个.conf文件,然后读取第二个.conf文件。 那是对的吗? 什么

example.conf(Serverpilot标准configuration文件)包含以下内容:

server { listen 80; listen [::]:80; server_name server-example example.net www.example.net ; root /srv/users/serverpilot/apps/example/public; access_log /srv/users/serverpilot/log/example/example_nginx.access.log main; error_log /srv/users/serverpilot/log/example/example_nginx.error.log; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; include /etc/nginx-sp/vhosts.d/example.d/*.nonssl_conf; include /etc/nginx-sp/vhosts.d/example.d/*.conf; } 

我创build了一个包含以下内容的example.ssl.conf:

 server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name example.net www.example.net; ssl on; # certificates ssl_certificate /etc/nginx-sp/certs/example.net/example.net.chained.crt; ssl_certificate_key /etc/nginx-sp/certs/example.net/example.net.key; #SSL Optimization ssl_session_timeout 1d; ssl_session_cache shared:SSL:20m; ssl_session_tickets off; # modern configuration ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK'; # OCSP stapling ssl_stapling on; ssl_stapling_verify on; # verify chain of trust of OCSP response ssl_trusted_certificate /etc/nginx-sp/certs/example.net/example.net.chained.crt; #root directory and logfiles root /srv/users/serverpilot/apps/example/public; access_log /srv/users/serverpilot/log/example/example_nginx.access.log main; error_log /srv/users/serverpilot/log/example/example_nginx.error.log; #proxyset proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-SSL on; proxy_set_header X-Forwarded-Proto $scheme; #includes include /etc/nginx-sp/vhosts.d/example.d/*.nonssl_conf; include /etc/nginx-sp/vhosts.d/example.d/*.conf; } 

我用这个configurationtesting了redirect链

我已经通过在https://httpstatus.io/中填写所有可能的url组合来testingredirect。 这是testing结果:

 url statuscodes url with http + www 301 → 200 (1 redirect) url with http 301 → 200 (1 redirect) url with https + www 301 → 200 (1 redirect) url with https 200 (0 redirect) 

我的问题是:从http + www到https的redirect是一步完成的。 它是否正确? 如果我testing其他几个网站,总是有两个redirect进行两步。 所以在我的情况下,从http + www的URLredirect应该是:301→301→200(2redirect)

我在某个地方犯了错吗? 或者这是最佳解决scheme?

我希望你能帮助我。

亲切的问候,1月

目标

  1. redirect应限制为最大。 1步骤(301→200)
  2. 服务器应尽可能快地执行redirect
  3. configuration文件应该从serverpilot更新保护,所以当serverpilot更新它的系统,没有文件被覆盖,不会发生错误。
  4. redirect应该被最常见的浏览器(Chrome,Firefox,IE,Safari)所接受,

我读过一篇文章: https : //serverfault.com/a/258424/377649 ,“最好的办法是使用三个服务器块:一个redirecthttp到https,一个redirecthttps www-name到没有www,一个实际处理请求。

你为什么在玩.htaccess文件? 这是Apache,而不是Nginx。

以下是您通常在Nginx中执行重写规则的方法。 这进入您的网站configuration文件。

 location /path/ { return 301 https://www.example.com/path/; } 

以下是如何redirect到根域,您使用两个位置块

 server { listen 443 ssl http2; # http2 only if you built that module in server_name www.example.com; ssl_certificate /path/to/file; ssl_certificate_key /path/to/file; # Insert other SSL configuration return 301 https://example.com$request_uri; } server { listen 80; server_name example.com www.example.com; return 301 https://example.com$request_uri; }