目前,我的nginx配置为将端口80重定向到端口443。
server {
listen [::]:443 ssl
listen 443 ssl;
root /var/www/my_app/public;
server_name example.com www.example.com;
# rest of my main config
}
# Here is port 80 listen
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
}如果我注释掉第二个服务器块,并将listen 80 default_server; listen [::]:80 default_server;添加到上面的块中,它允许http和https访问。
我想要实现的是,如果url包含example.com/api/*,不要将http重定向到https,而对于其余的url,则重定向到https。
如何使用nginx实现这一目标?我是否需要有两个服务器块,并在一个单独的文件中移动共享逻辑,并为每个端口分别考虑它们?
发布于 2020-08-05 15:32:28
对于端口80使用以下块,而不是您拥有的块:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/my_app/public;
location /api {
# API specific directives
}
location / {
return 301 https://www.example.com;
}
}https://serverfault.com/questions/1028970
复制相似问题