用于同时反向代理和服务php文件的nginx服务器块configuration

我configuration了icecast2服务器,通过nginx反向代理,因为icecast完全不支持ssl。 我希望能够反向代理icecast提供的文件,并在同一时间内运行同一域名内的另一个位置的PHP。 例如proxiedicecast.org显示icecast服务的文件和proxiedicecast.org/status我可以服务其他内容。 我有默认的服务器块configuration,适用于icecast,但是当我尝试从“proxiedicecast.org/status”浏览器访问php文件只是下载PHP文件,而不是执行它们。

server { listen 80; server_name proxiedicecast.org; index index.php index.html index.htm index.nginx-debian.html; location ~ /.well-known { allow all; } location / { if ($ssl_protocol = "") { rewrite ^ https://$server_name$request_uri? permanent; } } # php location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location ~ /\.ht { deny all; } } #### SSL ###################################################### server { #ssl on; ssl_dhparam /etc/ssl/certs/dhparam.pem; listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/proxiedicecast.org/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/proxiedicecast.org/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot # Recommended security settings from https://wiki.mozilla.org/Security /Server_Side_TLS # ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ecdh_curve secp384r1; # ssl_session_timeout 5m; ssl_session_cache shared:SSL:5m; ssl_session_tickets off; ssl_stapling on; ssl_stapling_verify on; resolver 8.8.8.8 8.8.4.4 valid=300s; resolver_timeout 5s; # Enable this if you want HSTS (recommended) # With or without preload (without very secure but not recommended) # add_header Strict-Transport-Security "max-age=15768000; includeSubdomains;" # add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload;" add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; root /var/www/html; server_name proxiedicecast.org; location ~ /.well-known { allow all; } location ~ /status { root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; try_files $uri $uri/ =404; } location / { #access_log /var/log/icecast/access_https.log icecast_combined; proxy_pass http://127.0.0.1:8000/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # php location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location ~ /\.ht { deny all; } } 

我build议你使用这个location块作为你的状态页面:

 location /status { rewrite ^ /status/index.php last; } 

也就是说,如果你的状态index.php位于/var/www/html/status/index.php

这里没有必要使用正则expression式修饰符来location块,因为你不要匹配*/status*/ ,任何以/status开头的简单的前缀匹配就足够了。

然后,在location块内,我们将请求重写为index.php ,nginx将启动与重写的URI匹配的位置。 这将被传递给location ~ \.php$ ,因为对于同一个请求,没有另一个冲突的正则expression式匹配。

有关nginx如何处理location指令的更多信息,请查看http://nginx.org/en/docs/http/ngx_http_core_module.html#location