我想为phpinfo.php
文件配置一个位置,/phpinfo
并为 phpPgAdmin 配置另一个位置/phppgadmin
。我的服务器根目录位于/usr/share/nginx/html/
,在那里我有一个index.html
和phpinfo.php
文件,phpPgAdmin 位于 ,/usr/share/phppgadmin
其index.php
文件位于 内。
这是我的default.conf
文件:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /phpinfo {
index phpinfo.php;
}
location /phppgadmin {
alias /usr/share/phppgadmin;
}
}
我为每个位置都设置了一个位置块,但都不起作用。
基本上我想要的是将所有这些配置为转到我的 php 信息http://localhost/phpinfo
和我的 phpPgAdmin 数据库管理器http://localhost/phppgadmin
。
答案1
让我们首先来看这个最小的工作示例:
server {
listen 80;
server_name test.sc;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location /phpinfo {
alias /usr/share/phppgadmin/;
index phpinfo.php;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
这里的问题是,通过使用
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
FPM 传递 /usr/share/nginx/html/phpinfo.php 进行处理,因为已定义带有“root”指令的 /usr/share/nginx/html。这将是“未指定输入文件。”错误。
如何操作:
server {
listen 80;
server_name test.sc;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location /phpinfo {
alias /usr/share/phppgadmin/;
index phpinfo.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
这样,/usr/share/phppgadmin/ 目录中的 php 文件将被单独处理,您甚至可以在 /usr/share/phppgadmin/ 上下文中为这些文件分配不同的 FPM 池。其他所有内容将使用下面的默认值进行处理。Nginx 仅限于此行为,因为将使用第一个正则表达式匹配块,它不会继续尝试任何进一步的操作或使用多个位置块。
现在您可以根据需要扩展配置:
server {
listen 80;
server_name test.sc;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location /phpinfo {
alias /usr/share/phppgadmin/;
index phpinfo.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location /phppgadmin {
alias /usr/share/phppgadmin;
index index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
此外,您可以通过重复使用某些部分来缩短此配置:
在 /etc/nginx/php_settings 中:
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
/etc/nginx/sites-enabled/000.conf:
server {
listen 80;
server_name test.sc;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location /phpinfo {
alias /usr/share/phppgadmin/;
index phpinfo.php;
include php_settings;
}
location /phppgadmin {
alias /usr/share/phppgadmin;
index index.php;
include php_settings;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}