简体   繁体   English

Nginx重定向http和https

[英]Nginx redirect for http and https

I am trying to force HTTPS for incoming connections while also redirecting all requests to a certain url. 我正在尝试强制HTTPS进行传入连接,同时还将所有请求重定向到某个URL。

Desired result: 所需结果:

http://example.com -> https://example.com/dir http://example.com- > https://example.com/dir

https://example.com -> https://example.com/dir https://example.com- > https://example.com/dir

Here is what I believe should work but is saying there are too many redirects. 这是我认为应该可以使用的方法,但是却说重定向太多。

server {
    listen       80;
    listen       443 ssl;
    server_name example.com;

   location / {
    return         301 https://$server_name/dir$request_uri;
   }

   location /dir {
       try_files $uri $uri/ /index.php?$args;
   }

Any help is much appreciated! 任何帮助深表感谢!

正在侦听443端口的服务器不应重定向到其自身(返回301 https://。。

Can you try this, 你可以试试这个吗

server {
    listen 80;
    listen 443;
    server_name example.com;

    location / {
      return 301 https://example.com/dir$request_uri; 
    }

    location /dir {
      ...
      ...
    }

}

Basically the idea is to keep both listen directive in the same server block. 基本上,想法是将两个listen指令都保留在同一服务器块中。 The above code is just an excerpt and you need to fill in the missing pieces.You can refer the return directive section of this link for more detail 上面的代码只是摘录,您需要填写缺少的部分。您可以参考链接的return指令部分以获取更多详细信息

EDIT: Updated the code with location block. 编辑:用位置块更新了代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM