Nginx http 重定向到 https 不起作用
Nginx http redirect to https doesn't work
我正在为我的石墨网使用 nginx 反向代理。
我的 nginx.conf 看起来有点像这样
server {
listen 8081;
server_name myserver.com;
return 301 https://$host:5000$request_uri;
}
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;
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;
}
我在 https://host:5000 and i want any http requests to this site to be redirected as a https request. As of now i see that it gets redirected to http://host/
上开始使用我的网站
这仅在登录和注销期间发生。
我看到很多示例,他们使用端口 80 来侦听 http 流量,但我不能使用该端口,因为其他应用程序已经在使用该端口。
有人可以帮我解决这个问题吗?
尝试使用 $server_name
而不是 $host
,例如:
server {
listen 8081;
server_name myserver.com;
return 301 https://$server_name:5000$request_uri;
}
您确实可以将您的域直接放在重定向上,例如:
return 301 https://myserver.com:5000$request_uri;
可能这不是你的情况,但由于你使用不同的端口可能值得检查 error_page 497
,这主要用于当你想要处理已发送到 HTTPS 端口的纯 HTTP 请求时:
server {
listen 8081 ssl;
server_name your.site.tld;
ssl on;
...
error_page 497 https://$host:5000$request_uri;
...
}
要了解有关 497
的更多信息,请查看 http://nginx.org/en/docs/http/ngx_http_ssl_module.html
中的 "Error Processing" 部分
我正在为我的石墨网使用 nginx 反向代理。 我的 nginx.conf 看起来有点像这样
server {
listen 8081;
server_name myserver.com;
return 301 https://$host:5000$request_uri;
}
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;
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;
}
我在 https://host:5000 and i want any http requests to this site to be redirected as a https request. As of now i see that it gets redirected to http://host/
上开始使用我的网站这仅在登录和注销期间发生。
我看到很多示例,他们使用端口 80 来侦听 http 流量,但我不能使用该端口,因为其他应用程序已经在使用该端口。
有人可以帮我解决这个问题吗?
尝试使用 $server_name
而不是 $host
,例如:
server {
listen 8081;
server_name myserver.com;
return 301 https://$server_name:5000$request_uri;
}
您确实可以将您的域直接放在重定向上,例如:
return 301 https://myserver.com:5000$request_uri;
可能这不是你的情况,但由于你使用不同的端口可能值得检查 error_page 497
,这主要用于当你想要处理已发送到 HTTPS 端口的纯 HTTP 请求时:
server {
listen 8081 ssl;
server_name your.site.tld;
ssl on;
...
error_page 497 https://$host:5000$request_uri;
...
}
要了解有关 497
的更多信息,请查看 http://nginx.org/en/docs/http/ngx_http_ssl_module.html