Termux下Nginx+PHP-FPM访问URL自动跳转8443端口的问题求助
解决Termux中Nginx+PHP-FPM访问URL自动重定向到8443端口的问题
核心原因
问题出在Nginx配置中存在强制跳转到8443端口的规则,或者HTTPS服务使用了非默认的443端口导致浏览器显示端口号。下面分两种场景给出解决方案:
场景1:不需要HTTPS,直接用80端口提供HTTP服务
找到并编辑Nginx站点配置文件
Termux中Nginx的站点配置通常位于$PREFIX/etc/nginx/sites-available/default,或者直接在主配置文件$PREFIX/etc/nginx/nginx.conf的server块中修改。删除/注释重定向到8443的规则
找到监听80端口的server块,删除类似以下的重定向代码:return 301 https://$server_name:8443$request_uri;确保最终的
server块配置如下(替换/path/to/your/webroot为你的网站根目录):server { listen 80; server_name localhost; root /path/to/your/webroot; index index.php index.html; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_pass unix:$PREFIX/var/run/php-fpm.sock; # 或127.0.0.1:9000,根据php-fpm配置调整 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }验证配置并重启Nginx
先检查配置语法是否正确:nginx -t无错误后重启服务:
nginx -s reload
场景2:需要HTTPS,隐藏端口号(使用默认443端口)
Termux默认限制普通程序绑定1024以下端口,需要先给Nginx授权:
给Nginx添加绑定低端口权限
sudo setcap cap_net_bind_service=+ep $PREFIX/bin/nginx修改Nginx的HTTPS配置
编辑站点配置文件,将HTTPS服务的监听端口从8443改为443,并调整80端口的重定向规则(不要带端口号):# 处理HTTP请求,重定向到HTTPS server { listen 80; server_name localhost; return 301 https://$server_name$request_uri; # 此处不要添加:8443 } # HTTPS服务配置 server { listen 443 ssl; server_name localhost; root /path/to/your/webroot; index index.php index.html; # 替换为你的SSL证书路径 ssl_certificate /path/to/your/cert.pem; ssl_certificate_key /path/to/your/key.pem; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_pass unix:$PREFIX/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }验证并重启服务
同样先执行nginx -t检查配置,无误后重启Nginx:nginx -s reload
额外检查项
- 检查PHP-FPM配置文件(通常在
$PREFIX/etc/php-fpm.d/www.conf),确保fastcgi_param SERVER_PORT设置为当前使用的端口(80或443),避免PHP脚本生成带8443的链接。 - 测试时使用浏览器隐私窗口,避免旧的重定向缓存影响结果。
内容的提问来源于stack exchange,提问作者Air Li
相关产品推荐
相关产品推荐

