Nginx反向代理:将所有HTTP请求重定向到https
我有一个反向代理,Nginx在端口5000上运行,我希望将所有到端口5000的请求重定向为https请求。Nginx反向代理:将所有HTTP请求重定向到https
现在我得到的错误:400错误请求的普通HTTP请求发送到HTTPS端口
server {
listen 5000 ssl;
server_name myserver.com;
location/{
proxy_pass http://127.0.0.1:8080;
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 X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host $host:5000;
add_header 'Access-Control-Allow-Methods' 'GET, POST';
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
add_header 'Access-Control-Allow-Credentials' 'true';
# here comes the basic auth, after the options part
auth_basic 'Restricted';
auth_basic_user_file path/to/.htpasswd;
}
ssl on;
ssl_certificate path/to/crt;
ssl_certificate_key path/to/key;
}
嗯,我试图用增加
if ($scheme != "https") {
rewrite^https://$host$request_uri permanent;
}
if ($scheme != "https") {
return 301 https://$host$request_uri permanent;
}
似乎没有任何解决问题。我应该怎么做才能解决这个问题?
好,使用error_page似乎做的伎俩我。
server {
listen 5000 ssl;
server_name myserver.com;
error_page 497 https://$host:5000$request_uri;
..
..
}
为了更多地了解的497检查在http://nginx.org/en/docs/http/ngx_http_ssl_module.html “错误处理” 部分
这可能听起来很简单,但您是否曾尝试将“https://”添加到您键入的域名的前面?我发现默认总是一个普通的“http”请求。
是的,但一段时间后,对于某些请求页面刷新和'https://'被删除,我着陆在上面提到的错误页面 – InfamousCoder
然后,下面的答案是你想 - 做一个自动重定向到ssl url。 –
我是否使用位置内的proxy_redirect? – InfamousCoder
假设HTTP流量来自通过80端口,就可以通过添加额外的服务器块监听到该端口重定向到https:
server {
listen 80;
server_name myserver.com;
location/{
return 301 https://myserver.com$request_uri;
}
}
的可能的复制[nginx的重定向HTTPS到HTTP](https://stackoverflow.com/questions/3470290/nginx- redirect-https-http) – lifeisfoo