在位置内部使用别名 [英] Working with alias inside location
问题描述
这是我的服务器块
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld/html;
index index.php index.html index.htm;
location / {
}
location /phpmyadmin {
alias /var/www/phpmyadmin;
}
location /nginx_status {
stub_status on;
access_log off;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
浏览 http://domain.tld/index.php 效果很好,唯一遇到的问题是浏览 http://domain.tld/phpmyadmin/.它返回404,但服务器上存在/var/www/phpmyadmin文件夹.查看/var/log/nginx/error.log,没有错误被记录在那里,但对它的访问已记录在/var/log/nginx/access.log中.这可能是什么问题?
browsing http://domain.tld/index.php works fine the only problem im encountering is browsing http://domain.tld/phpmyadmin/. It returns 404 yet the folder /var/www/phpmyadmin exist on the server. Viewing /var/log/nginx/error.log, no error is being logged there yet the access to it is logged in /var/log/nginx/access.log. What could be the problem here?
推荐答案
问题是phpmyadmin是一个PHP应用程序,您的location ~ \.php$
块未指向正确的文档根目录.
The problem is that phpmyadmin is a PHP application and your location ~ \.php$
block does not point to the correct document root.
您需要构造两个具有不同文档根目录的PHP位置.
You need to construct two PHP locations with different document roots.
如果phpmyadmin位于/var/www/phpmyadmin
,则不需要alias
指令,因为root
指令会更有效.参见本文档.
If phpmyadmin is located at /var/www/phpmyadmin
, you do not need an alias
directive, as a root
directive will be more efficient. See this document.
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld/html;
index index.php index.html index.htm;
location / {
}
location /nginx_status {
stub_status on;
access_log off;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ^~ /phpmyadmin {
root /var/www;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
location ^~ /phpmyadmin
是一个前缀位置,它优先于通常用于处理.php
文件的正则表达式位置.它包含一个location ~ \.php$
块,该块继承了文档根目录的/var/www
值.
The location ^~ /phpmyadmin
is a prefix location that takes precedence over the regex location normally used to process .php
files. It contains a location ~ \.php$
block which inherits a value of /var/www
for the document root.
建议在定义其他fastcgi_param
参数之前,先include fastcgi_params
,否则您的自定义值可能会被静默覆盖.
It is advisable to include fastcgi_params
before defining other fastcgi_param
parameters otherwise your custom values may be silently overwritten.
有关更多信息,请参见此文档.
See this document for more.
这篇关于在位置内部使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!