gpt4 book ai didi

ruby-on-rails - 使用 SSL 在 Nginx 上将 www 重定向到非 www 会产生重定向循环

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:49 26 4
gpt4 key购买 nike

如果这看起来像是似曾相识,我深表歉意。有很多关于类似问题的帖子,我都阅读了它们(并尝试了它们但没有成功)。

我的设置:Rails 4、Puma、Nginx、两者的 SSL 证书 https://www和 https://

我使用的是组合 block ,所以我得到了到 SSL 的重定向。但是,我想重定向 https://www.domain.comhttps://domain.com在我添加重定向规则(返回 301 https://$host$request_uri;)之前,您将在下面看到的设置一切正常,然后我得到一个重定向循环。

我添加了“proxy_set_header X-Forwarded-Proto $scheme;”到我的@app 位置 forforce_ssl(在 Rails 配置文件中设置为 true),但这并没有解决问题。

在此非常感谢专家建议,如果您发现我的设置有任何改进之处,除了修复重定向循环之外,请告诉我。

nginx.conf:

user root;
worker_processes 4;
pid /var/run/nginx.pid;

#setup where nginx will log errors to
# and where the nginx process id resides
error_log /var/log/nginx/error.log error;
#pid /var/run/nginx.pid;

events {
worker_connections 1024;
accept_mutex off;
use epoll;
}


http {
include /etc/nginx/mime.types;
types_hash_max_size 2048;
default_type application/octet-stream;
#access_log /tmp/nginx.access.log combined;

# use the kernel sendfile
sendfile on;
# prepend http headers before sendfile()
tcp_nopush on;

keepalive_timeout 25;
tcp_nodelay on;

gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;

#Hide server info
server_tokens off;

upstream app_server {
server unix:/root/sites/mina_deploy/shared/tmp/sockets/puma.sock
fail_timeout=0;
}

# configure the virtual host
server {

server_name domain.com www.domain.com 162.555.555.162;

root /root/sites/mina_deploy/current/public;
# port to listen for requests on
listen 80 default deferred;
listen 443 ssl;

####### THIS REDIRECT CAUSES A LOOP ########
#return 301 https://$host$request_uri;

ssl_certificate /etc/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/ssl/myserver.key;
#enables all versions of TLS, but not SSLv2 or 3 which are weak and now deprecated.
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#Disables all weak ciphers
ssl_ciphers 'AES128+EECDH:AES128+EDH';
ssl_session_cache shared:SSL:10m;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/dhparam.pem;
# maximum accepted body size of client request
client_max_body_size 4G;
# the server will close connections after this time
keepalive_timeout 5;
add_header Strict-Transport-Security max-age=63072000;
#add_header X-Frame-Options DENY;
add_header Access-Control-Allow-Origin '*';
add_header X-Content-Type-Options nosniff;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;

location ~ ^/(system|assets)/ {
gzip_static on;
error_page 405 = $uri;
expires max;
add_header Cache-Control public;
break;
}

try_files $uri/index.html $uri @app;

location @app {
# pass to the upstream unicorn server mentioned above
proxy_pass http://app_server;
proxy_redirect off;

proxy_set_header Host $host;
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_read_timeout 300;
}
}
}

最佳答案

我做的是有多个服务器 block 。您提到您希望 www.domain.com 重定向到 domain.com。在这种情况下我会做

server {
listen 80;
server_name www.domain.com;
return 301 https://domain.com$request_uri;
}

然后从您的原始 block 中的 server_name 中删除您的 www.domain.com。此外,我还将在单独的 block 中将您的重定向从 80 拆分为 443。因此,如果用户试图转到 https://www.domain.com,您将重复此过程。你会有一个服务器说类似的话。

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

还有一个用于监听您想要的域上的 http 流量,但重定向到 https 流量。

server {
listen 80;
server_name domain.com;
return 301 https://domain.com$request_uri;
}

然后您可以在您希望每个人都去的服务器 block 中只监听端口 443,并且该 block 中没有重定向。

您可以查看 nginx 的文档 here这将告诉你这是重写的正确方法

回复你的评论,使用我写的三个 block ,在你原来的服务器 block 中,你需要删除

server_name domain.com www.domain.com 162.555.555.162;

也删除

listen 80 deferred;

并添加

server_name domain.com;

此外,只要确保您知道要使其正常工作,您必须将您的域和 www 子域指向您的服务器

关于ruby-on-rails - 使用 SSL 在 Nginx 上将 www 重定向到非 www 会产生重定向循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31523173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com