簡體   English   中英

Nginx重定向http和https

[英]Nginx redirect for http and https

我正在嘗試強制HTTPS進行傳入連接,同時還將所有請求重定向到某個URL。

所需結果:

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

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

這是我認為應該可以使用的方法,但是卻說重定向太多。

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;
   }

任何幫助深表感謝!

正在偵聽443端口的服務器不應重定向到其自身(返回301 https://。。

你可以試試這個嗎

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

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

    location /dir {
      ...
      ...
    }

}

基本上,想法是將兩個listen指令都保留在同一服務器塊中。 上面的代碼只是摘錄,您需要填寫缺少的部分。您可以參考鏈接的return指令部分以獲取更多詳細信息

編輯:用位置塊更新了代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM