如果目录不存在则查找文件 nginx
Look for file if directory doesn't exist nginx
我正在将一个旧网站从盒子 运行 apache 转移到一个新的 运行 nginx。旧站点使用 .htaccess
规则,我发现很难将其转换为 nginx 配置语法。
具体来说,如果在旧站点上向 example.com/video
发出请求,那么如果没有目录 video
,它会加载 video.php
或 video.html
文件.
以下是 .htaccess
规则:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) \.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) \.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .? /index.html [L]
我在 nginx 中尝试了以下但没有成功 (404):
server {
listen 80;
server_name example.org www.example.org;
access_log /var/log/example.org/access.log;
error_log /var/log/example.org/error.log;
root /var/www/vhosts/example.org/;
index index.html index.htm;
location / {
rewrite ^(.*)$ /\.php break;
rewrite ^(.*)$ /\.html break;
if (!-e $request_filename){
rewrite .? /index.html break;
}
}
location /app {
rewrite ^/app/? /join?p=getApp redirect;
}
}
try_files
指令专为这种情况而制定:
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
您的 nginx 配置将如下所示:
server {
listen 80;
server_name example.org www.example.org;
access_log /var/log/example.org/access.log;
error_log /var/log/example.org/error.log;
root /var/www/vhosts/example.org/;
index index.html index.htm;
location / {
try_files $uri $uri.php $uri.html;
}
location /app {
rewrite ^/app/? /join?p=getApp redirect;
}
我正在将一个旧网站从盒子 运行 apache 转移到一个新的 运行 nginx。旧站点使用 .htaccess
规则,我发现很难将其转换为 nginx 配置语法。
具体来说,如果在旧站点上向 example.com/video
发出请求,那么如果没有目录 video
,它会加载 video.php
或 video.html
文件.
以下是 .htaccess
规则:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) \.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) \.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .? /index.html [L]
我在 nginx 中尝试了以下但没有成功 (404):
server {
listen 80;
server_name example.org www.example.org;
access_log /var/log/example.org/access.log;
error_log /var/log/example.org/error.log;
root /var/www/vhosts/example.org/;
index index.html index.htm;
location / {
rewrite ^(.*)$ /\.php break;
rewrite ^(.*)$ /\.html break;
if (!-e $request_filename){
rewrite .? /index.html break;
}
}
location /app {
rewrite ^/app/? /join?p=getApp redirect;
}
}
try_files
指令专为这种情况而制定:
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
您的 nginx 配置将如下所示:
server {
listen 80;
server_name example.org www.example.org;
access_log /var/log/example.org/access.log;
error_log /var/log/example.org/error.log;
root /var/www/vhosts/example.org/;
index index.html index.htm;
location / {
try_files $uri $uri.php $uri.html;
}
location /app {
rewrite ^/app/? /join?p=getApp redirect;
}