我想将路径从http重定向到https,如下所示:
- http://localhost:80/到相同的Http url
- http://localhost:80/api/到https://localhost:80/api/它又重定向到https://localhost:55555/api/
我有一个配置文件:
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
listen [::]:80;
server_name localhost;
return 301 https://$host$request_uri;
}
# HTTPS server
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate certs/myservice.crt;
ssl_certificate_key certs/myservice.key;
server_name myservice.com localhost;
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass https://localhost:55555/api/;
client_max_body_size 500G;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 3600;
send_timeout 300;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:80/;
client_max_body_size 500G;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 3600;
send_timeout 300;
}
location ~ /.ht {
deny all;
}
}
}
当我尝试这样做时,第二个要求得到了满足。但第一个http://localhost:80/同样的失败。它被不必要地重定向为https://localhost。
简而言之,nginx将所有到达localhost服务器上端口80的HTTP请求重定向到HTTPS。
我还尝试从第二个服务器块中删除位置/{}部分。
然后尝试在第一个服务器块中指定为:
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://localhost:80/
}
location /api/ {
return 301 https://$host$request_uri;
}
}
他们两个都不起作用。
在Nginx中,将服务器的特定路径从http重定向到https的正确方法是什么?
第二个服务器块中的这一部分不起作用。因为它再次重定向到https。
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:80/;
client_max_body_size 500G;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 3600;
send_timeout 300;
}
因此,将该应用程序暴露到80以外的其他主机端口,比如88。然后将此代理程序URL更改为:
proxy_pass http://localhost:88/;
现在它运行良好。