使用别名内部位置 [英] 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
参数之前包含 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屋!