在AWS EC2负载均衡器后强制使用WWW

我提出了一个小问题,我们正在使用一个负载平衡器的新项目,但我们不能强制www。 在请求之间没有redirect循环。

我们目前正在使用NGINX,redirect的代码片段如下:


LOAD BALANCER NGINX CONFIG

# FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/mywebsite.com/before/*; # FORGE CONFIG (DOT NOT REMOVE!) include upstreams/mywebsite.com; server { listen 443 ssl; listen [::]:443 ssl; server_name .mywebsite.com; if ($host !~* ^www\.){ rewrite ^(.*)$ https://www.mywebsite.com$1; } # FORGE SSL (DO NOT REMOVE!) ssl_certificate /etc/nginx/ssl/mywebsite.com/225451/server.crt; ssl_certificate_key /etc/nginx/ssl/mywebsite.com/225451/server.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; charset utf-8; access_log off; error_log /var/log/nginx/mywebsite.com-error.log error; # FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/mywebsite.com/server/*; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://370308_app/; proxy_redirect off; # Handle Web Socket Connections proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } # FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/mywebsite.com/after/*; 

HTTP服务器NGINXconfiguration

 # FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/mywebsite.com/before/*; server { listen 80; listen [::]:80; server_name .mywebsite.com; root /home/forge/mywebsite.com/public; if ($host !~* ^www\.){ rewrite ^(.*)$ https://www.mywebsite.com$1; } # FORGE SSL (DO NOT REMOVE!) # ssl_certificate; # ssl_certificate_key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA'; ssl_prefer_server_ciphers on; ssl_dhparam /etc/nginx/dhparams.pem; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header X-Content-Type-Options "nosniff"; index index.html index.htm index.php; charset utf-8; # FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/mywebsite.com/server/*; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log off; error_log /var/log/nginx/mywebsite.com-error.log error; error_page 404 /index.php; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } } # FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/mywebsite.com/after/*; 

事情是,用这个configuration我只从服务器获取redirect循环。

请帮助:D <3

在写完先前的通用答案后,我搜索了“FORGE CONFIG(DOT NOT REMOVE!)” ,这是第一个结果:

https://laracasts.com/discuss/channels/forge/forge-how-to-disable-nginx-default-redirection

在nginx / forge-conf / be106.net / before / redirect.conf文件里面有这个简单的配置:

 … server_name www.my-domain.net; return 301 $scheme://my-domain.net$request_uri; … 

有没有一个简单的方法来消除这个没有改变文件本身(因为它看起来像个坏主意)。

所以,重定向似乎是由您正在使用的应用程序引起的,所以我们找到了循环的最可能原因!


反过来,配置您的应用程序以避免所述循环的适当方式将超出StackOverflow的分数。

但是,作为解决方法:

  • 考虑你是否真的需要在负载平衡器级别的所有forge-conf include指令; 随后,您可以伪造适当的域名传递给后端,不会导致重定向(提供您删除自己的冗余重定向):

     - proxy_set_header Host $http_host; + proxy_set_header Host example.com; 
  • 请注意, forge-conf/example.com/before/redirect.conf指令优先于您自己的.example.com配置的原因是指令的顺序 – 您可能会将/before/* include移动到之后你自己的配置,如果这样的举动否则是有道理的。

  • 我不认为你提供的nginx片段本身会导致重定向循环。

  • 首先,你必须弄清楚它是否是一个实际的重定向 – 在这些问题中,经常会出现301 Moved Permanently响应缓存在浏览器中,随后你会看到一个缓存的版本,而不是一个新的。

  • 随后,你必须找出是什么导致重定向循环:

    • 尝试添加独特的字符串到每个重定向指令,看看哪一个会导致循环。

       if ($host !~* ^www\.) {return 301 $scheme://www.$host/levelX$request_uri} 
    • 问自己,为什么你有这么多的重定向指令呢?在前端负载平衡器和后端都有重定向指令似乎没有什么正当的理由。

  • 如果上述内容不能解决问题,那么你知道重定向循环不是来自你提供的文件,你必须深入挖掘 – 它可能来自其他文件,也许你可能是其中一个include指令,或者www.example.com的默认服务器在其他地方定义,重定向到example.com ,或者重定向在应用程序层完成。