NGINX将Pterodactyl的PHP文件作为下载而非渲染的问题求助
问题:同一域名不同路径部署Pterodactyl与PhpMyAdmin,Pterodactyl的PHP文件被当作下载文件
在同一域名sub.example.com的不同路径下部署Pterodactyl(/pterodactyl)和PhpMyAdmin(/phpmyadmin),PhpMyAdmin的PHP解析正常,但Pterodactyl的PHP文件会被NGINX当作下载文件返回,而非解析渲染。以下是相关配置与环境信息:
站点配置文件(sub.example.com)
server { server_name sub.example.com; root /var/www/sub.example.com; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php; } # location = /favicon.ico { access_log off; log_not_found off; } # location = /robots.txt { access_log off; log_not_found off; } # PhpMyAdmin location ^~ /phpmyadmin { auth_basic "Admin Login"; auth_basic_user_file /etc/nginx/pma_pass; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; } } # This is the actual Pterodactyl path (code) location ^~ /pterodactyl { alias /var/www/sub.example.com/pterod/public; access_log off; error_log /var/log/nginx/pterodactyl.app-error.log error; client_max_body_size 100m; client_body_timeout 120s; sendfile off; location ~ { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/run/php/php8.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param PHP_VALUE "upload_max_filesize = 100M \n post_max_size=100M"; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTP_PROXY ""; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; } } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; } location ~ /\.ht { deny all; } listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/sub.example.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/sub.example.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = sub.example.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name sub.example.com; return 404; # managed by Certbot }
环境参数
| PHP版本 | php8.2-fpm.sock是否存在 | Ubuntu版本 | NGINX版本 |
|---|---|---|---|
| 8.2.6 | 是 | Ubuntu 22.04 | nginx/1.18.0 |
Pterodactyl路径配置来自官方指南,pteroq.service已启用,Redis、数据库运行正常。
nginx.conf
user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
snippets/fastcgi-php.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path fastcgi_split_path_info ^(.+?\.php)(/.*)$; # Check that the PHP script exists before passing it try_files $fastcgi_script_name =404; # Bypass the fact that try_files resets $fastcgi_path_info # see: http://trac.nginx.org/nginx/ticket/321 set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info; fastcgi_index index.php; include fastcgi.conf;
解决方案
问题出在/pterodactyl块内的子location匹配顺序、路径拼接规则,以及根路径的路由干扰上,修改如下:
1. 修正/pterodactyl配置块
location ^~ /pterodactyl { alias /var/www/sub.example.com/pterod/public; index index.php; access_log off; error_log /var/log/nginx/pterodactyl.app-error.log error; client_max_body_size 100m; client_body_timeout 120s; sendfile off; # 优先匹配PHP文件,确保解析规则不被拦截 location ~ \.php$ { fastcgi_split_path_info ^(/pterodactyl/.+\.php)(/.+)$; fastcgi_pass unix:/run/php/php8.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param PHP_VALUE "upload_max_filesize = 100M \n post_max_size=100M"; # 使用$request_filename适配alias路径,避免文件查找错误 fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_param HTTP_PROXY ""; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; } # 处理静态文件和路由跳转,优先级低于PHP解析 try_files $uri $uri/ /pterodactyl/index.php?$query_string; }
2. 修改根路径的路由规则
location / { try_files $uri $uri/ =404; }
3. 重启NGINX服务
sudo systemctl restart nginx
修改说明
- 调整子location顺序,让PHP解析规则优先匹配,避免被通用规则拦截。
- 使用
$request_filename替代$document_root$fastcgi_script_name,适配alias路径的文件查找逻辑。 - 根路径不再默认跳转至
/index.php,防止干扰子路径的独立路由。
内容的提问来源于stack exchange,提问作者Sip
相关产品推荐
相关产品推荐

