大多数人在浏览器栏中键入: mysite.com而不是https://mysite.com 。
包括我自己在内的许多开发者在他们的Nginxconfiguration文件中都有类似的东西,这意味着这个mysite.com请求会导致redirect到https:// site:
server { listen 80; server_name mysite.com; return 301 https://$server_name$request_uri; }
谷歌的网页速度团队最近表示,这些redirect对于移动性能来说尤其如此,因为redirect导致请求通过移动运营商networking返回。
我的问题是有没有其他的方式来编写nginx.conf,以便在server_nameinput的人不会遇到这个http://到https://redirect的惩罚?
不,这需要你改变浏览器的行为。 这都是基于请求响应的。 用户在他的浏览器栏中键入example.com ,浏览器自动在其前面添加http:// 。 因此,您的服务器将始终在http://example.com上获得第一个请求,如果没有SSL,则只能通过redirect到启用了SSL的地址进行应答。
否认这个要求,正如Nathan所提出的,绝对不行。 由于浏览器将显示错误页面,该网站无法访问,甚至可能不存在。
但是,您还可以做其他事情: HTTP严格传输安全性(HSTS)
HSTS告诉浏览器您的站点只能通过SSL访问,并且随后的请求应始终使用https://而不是http://自动完成。 你可以在你的SSL服务器块中用下面几行在nginx中实现:
add_header Strict-Transport-Security "max-age=262974383";
http { # One server listening on port 80 and sending the redirect to HTTPS server { server_name example.com; return 301 https://$server_name$request_uri; } # Our actual server handling incoming requests. server { listen 443 ssl; server_name example.com; ssl_certificate /etc/ssl/my_site.pem; ssl_certificate_key /etc/ssl/my_site.key; # Tell the browser that he should always visit us with SSL. add_header Strict-Transport-Security "max-age=262974383"; } }
或者不要听80端口(这将抛出一个错误),或拒绝请求:
server { listen 80; server_name mysite.com; location / { deny all; } }
然后,只需要你正常的443块。