【发布时间】:2019-07-09 13:00:34
【问题描述】:
我正在尝试将一些已经工作的重写规则从 .htaccess 转换为 nginx。 我的应用程序有 2 种模式,后端(所有以 /admin/* 开头的调用)和前端,其余的调用。后端请求被路由到 admin.php,而前端请求被路由到 index.php
这在 apache 中效果很好,但在 nginx 中我只能让前端路由工作。 /admin/ 请求确实调用了 admin.php 文件,但 php 文件被下载而不是被执行。我已经使用http://winginx.com 将我的 htaccess 路由转换为 nginx,但我仍然无法让它为 /admin 工作。
一位 nginx 专业人士可以帮助我进行正确的配置吗?
这是我的工作 .htaccess 配置:
<IfModule mod_rewrite.c>
#RewriteBase /
# Google sitemap.xml configuration
RewriteRule sitemap.xml$ /index.php?_extension=Frontend&_controller=Sitemap&action=googleSitemap [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
# admin routes
RewriteRule ^/admin/(.*)$ admin.php?%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# frontend routes
RewriteRule .* index.php [L]
</IfModule>
这是我目前尝试的 nginx 配置...
server {
listen 80;
server_name mydomain.local;
root /var/www/project;
index index.php index.html index.htm;
access_log /var/log/nginx/default-access.log main;
error_log /var/log/nginx/default-error.log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/default;
}
location / {
rewrite sitemap.xml$ /index.php?_extension=Frontend&_controller=Sitemap&action=googleSitemap redirect;
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^(.*)$ /index.php;
}
location /admin {
rewrite ^/admin/(.*)$ /admin.php?$query_string break;
}
location ~ \.php {
include fastcgi_params;
fastcgi_keep_conn on;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php-fpm.sock;
}
}
开启 nginx,做一个 curl -s -D - 'http://mydomain.local/frontend/call' | head -n 20 在调用 curl -s -D - 'http://cms.dev/admin/whatever' 时确实将 Content-type 作为 text/html 返回head -n 20 返回触发下载的 application/octet-stream 内容类型。
【问题讨论】:
标签: php apache .htaccess mod-rewrite nginx