我希望www.example.com跳转到 Node 应用程序,www.example.com/administration 跳转到 php。以下是 example.com 页面的当前配置文件:
# the configuration file
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl ipv6only=on;
server_name example.com www.example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log warn;
index index.html index.htm index.php;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location /administration {
try_files $uri $uri/ /var/www/example.com/administration/index.html;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
它适用于 www.example.com(启动 Node 应用程序),但我不知道如何设置 PHP 部分。
答案1
除了设置try_files
和基本 HTTP 身份验证之外,您还没有为/administration
位置块配置任何内容。您应该告诉 nginx 将请求传递给 PHP 解释器(如 php-fpm)。最常见的方法是将任何请求传递给以 结尾的请求到.php
PHP 解释器,如下所示:
location ~* \.php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
的值fastcgi_pass
可能因您的系统而异。它可能是不同的套接字,也可能是 TCP 端口,例如127.0.0.1:9000
(这也是相当常见的)。这取决于您的设置,但您的问题中没有足够的信息来告诉您它应该是什么。