nginx - 如何在同一目录中使用 'try_files' 和 'index' 语句?
nginx - how to use 'try_files' and an 'index' statement in the same directory?
landing.php
和我站点的所有其他页面都在同一目录中。我希望能够访问两者,但是如果我有一个 try_files
语句作为 以及 作为一个 index
语句,我得到一个 404 error
当访问索引页。
有没有办法在不冲突的情况下实现这一点?
这是我的配置文件:
server {
listen 80;
access_log /var/www/mysite.com/logs/access.log;
error_log /var/www/mysite.com/logs/error.log;
root /var/www/mysite.com/public_html/mysite/public/_main;
server_name www.mysite.com mysite.com;
location / {
index landing.php;
try_files $uri $uri.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index landing.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
尝试以下对我有用的配置:
server {
root /path/to/root;
index index.html index.htm;
server_name subdomain.domain.com;
location / {
try_files $uri $uri/ /index.html;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
}
}
我终于想出了如何做到这一点。你需要的是两个位置块,像这样:
location = / {
index landing.php;
}
location / {
try_files $uri $uri.php?$query_string;
}
这样做是将 try_files
用于任何其他页面,例如 www.site.com/somepage
;但是,如果 URL 是 www.site.com
,它将获取索引页。
landing.php
和我站点的所有其他页面都在同一目录中。我希望能够访问两者,但是如果我有一个 try_files
语句作为 以及 作为一个 index
语句,我得到一个 404 error
当访问索引页。
有没有办法在不冲突的情况下实现这一点?
这是我的配置文件:
server {
listen 80;
access_log /var/www/mysite.com/logs/access.log;
error_log /var/www/mysite.com/logs/error.log;
root /var/www/mysite.com/public_html/mysite/public/_main;
server_name www.mysite.com mysite.com;
location / {
index landing.php;
try_files $uri $uri.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index landing.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
尝试以下对我有用的配置:
server {
root /path/to/root;
index index.html index.htm;
server_name subdomain.domain.com;
location / {
try_files $uri $uri/ /index.html;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
}
}
我终于想出了如何做到这一点。你需要的是两个位置块,像这样:
location = / {
index landing.php;
}
location / {
try_files $uri $uri.php?$query_string;
}
这样做是将 try_files
用于任何其他页面,例如 www.site.com/somepage
;但是,如果 URL 是 www.site.com
,它将获取索引页。