gpt4 book ai didi

ssl - Nginx 反向代理力 https

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

我们有一个使用 nginx 的反向代理服务器设置。该服务器会将访问者重定向到他们访问路径中的相应服务器。现在,我想设置 nginx.conf 以强制将它们重定向到 https。问题是我收到“太多重定向”错误。我尝试使用重写并添加“proxy_set_header X-Forwarded-Proto https;”但都不起作用。我们有办法实现这一目标吗?我们已经在 cloudflare 上设置了一个 ssl 证书,所以我们不需要在配置中添加它。

以下是当前nginx的配置设置。

   server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;

if ($http_x_forwarded_proto = "http") {
rewrite ^/(.*)$ https://development-link/$1 permanent;
}

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://development-elb1;
proxy_set_header X-Forwarded-Proto https;
}

location /en {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://development-elb2;
proxy_set_header X-Forwarded-Proto https;
}

最佳答案

使用单独的 block 。

# Redirect HTTP to HTTPS
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
return 301 https://$host$request_uri;
}

# HTTPS
server {
listen 443 default_server;
listen [::]:443 default_server;
server_name localhost;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://development-elb1;
proxy_set_header X-Forwarded-Proto https;
}

location /en {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://development-elb2;
proxy_set_header X-Forwarded-Proto https;
}
}

关于ssl - Nginx 反向代理力 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131915/

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