簡體   English   中英

將https從非www重定向到www

[英]Redirect https from non www to www

以下是我的Nginx服務器配置(不起作用)。 我需要將https非www重定向到www。 因此,當我訪問網站https://example.com ,它應該將我重定向到https://www.example.com

server {
    listen 443 ssl
    server_name example.com;
    return 301 https://www.example.com$request_uri;
}

編輯:我的負載平衡器: 在此處輸入圖片說明

我正在使用在AWS中運行Ruby on Rails應用程序的AMI Linux。 謝謝!!

請在您的nginx.conf文件中使用以下配置。 它對我來說很好。

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

       location / {
            index       index.html;
            root        /usr/share/nginx/html;  #the location of your app folder
       }


        if ($http_x_forwarded_proto = 'http') {
            return 301 https://www.example.com$request_uri;
        }

}

如果您只想重定向GET請求並為非GET請求拋出405(不允許使用方法):

server {
  listen 443 ssl;
  server_name example.com;
  if ($request_method = GET) {
    return 301 https://www.example.com$request_uri;
  }
  return 405;
}

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

  # locations
}

如果您希望所有請求(GET,POST,...)都被重定向,這是不可能的,那么我建議您嘗試使用Akshath的方法。

暫無
暫無

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

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