简体   繁体   中英

Nginx redirect for http and https

I am trying to force HTTPS for incoming connections while also redirecting all requests to a certain url.

Desired result:

http://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. 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

EDIT: Updated the code with location block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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