最近,我遇到了 nginx 在重定向后放置端口号的问题,如果 URL 缺少尾部斜杠(例如:https://example.com/thing
将重定向到https://example:8080/thing/
)。因此,我添加了行port_in_redirect off;
并解决了该问题。但是,它现在又引发了另一个问题。如果 URL 缺少尾部斜杠,它会重定向到 HTTP。
https://example.com/thing
将重定向到http://example/thing/
,这会导致请求失败。
我的 nginx.conf 如下所示:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log /var/log/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format logstash_json '{ "@timestamp": "$time_iso8601", '
'"@fields": { '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"request": "$request", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"request_time": "$request_time", '
'"request_method": "$request_method", '
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent" } }';
access_log /var/log/access.log logstash_json;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080; # Port to listen on
server_name localhost; # Servername
client_max_body_size 0; # Max upload size
chunked_transfer_encoding on; # Support for chunked transfer (upload)
port_in_redirect off;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#include servers/*;
}
答案1
默认情况下,nginx
在 3xx 响应中发出绝对 URL,其中包括用于连接服务器的方案。您的服务器在端口 8080 上连接到http
,因此这就是 3xx 响应中出现的方案。
自 1.11.8 版本以来,nginx
可以配置为发出相对 URL,从而从 URL 中删除方案和主机名。
absolute_redirect off;
看这个文件了解详情。
如果您使用的是旧版本的nginx
(并且无法升级)您可能能够使用显式if...return
语句来覆盖默认行为。
您现有的配置看起来相当简单:
location / {
root html;
index index.html index.htm;
}
有许多边缘情况,因此解决方案可能变得非常复杂,但类似这样的方法可能适合您:
root html;
location ~ /$ {
try_files "${uri}index.html" "${uri}index.htm" =404;
}
location / {
try_files $uri @rewrite;
}
location @rewrite {
if (-d $request_filename) {
return https://$host$uri/$is_args$args;
}
}