我正在尝试从http重新路由到https。 例如,您访问example.com,您将被自动redirect到https://example.com 。
我尝试使用这一个:
server { listen 80; return 301 https://$host$request_uri; }
以及这一个:
server { listen 80; server_name example.com; return 301 https://$server_name$request_uri; }
在这里find: 在Nginx,我怎么能重写所有http请求,同时维护子域?
但是,似乎对我来说都不是什么东西。 我留在example.com上。
任何人有一个想法?
您尚未为您的主机定义服务器名称。
server { listen 80; server_name *.example.com example.com; return 301 https://$host$request_uri; }
否则你的主机不被调用。 当你看这个例子时,你可以看到在这两种情况下都有一个服务器名称。
你的第二个例子将工作的任何请求去http://example.com ,但请记住,www.example.com和example.com是不同的,所以如果你需要redirect的任何事情在example.com你可以做
server { listen 80; server_name *.example.com; return 301 https://$server_name$request_uri; }
否则redirect每个主机,即
server { listen 80; server_name www.example.com example.com images.example.com cdn.example.com; return 301 https://$server_name$request_uri; }
确保你通过nginx -ttesting了configuration,当你通过nginx重装进行修改时重新加载configuration。 你可以testing你通过HTTP标头或curl获得什么
下面的输出是当我尝试一个http头请求到我们直接https访问上面的确切的服务器块时看到的。
$ curl -I -L http://host.domain.com HTTP/1.1 301 Moved Permanently Server: nginx/1.6.2 Date: Mon, 06 Apr 2015 03:26:39 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: https://host.domain.com/ HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Mon, 06 Apr 2015 03:26:41 GMT Content-Type: text/html; charset=utf-8 Connection: keep-alive Vary: Accept-Encoding X-Powered-By: PHP/5.5.22 Set-Cookie: PHPSESSID=dca72682392e7ac96d4b7703ea7a1eb1; path=/; domain=domain.com; secure; HttpOnly Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: PHPSESSID=c910822f8007fe8c0424715a24aa4728; path=/; domain=domain.com; secure; HttpOnly
我也有类似的错误 而不是redirect页面保持404。
原来是configuration之间的冲突。
我的configuration被放在/etc/nginx/conf.d/中。 我没有注意到的是,在/ etc / nginx / sites-enabled /默认的configuration位于80端口,这比我conf在conf.d有更高的优先权。 简单地通过删除默认configuration解决了我的问题,redirect工作正常。