![在 nginx 服务器上托管 Stacey?(将 .htaccess 转换为 nginx)](https://linux22.com/image/604941/%E5%9C%A8%20nginx%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E6%89%98%E7%AE%A1%20Stacey%EF%BC%9F%EF%BC%88%E5%B0%86%20.htaccess%20%E8%BD%AC%E6%8D%A2%E4%B8%BA%20nginx%EF%BC%89.png)
史黛西是一个轻量级内容管理系统。我在我的作品集网站上使用它,但它使用 .htaccess 文件来创建友好的 URL。是否可以重写此 .htaccess 行以在 nginx 中使用?以下是 .htaccess 文件内容;
RewriteEngine on
# Some hosts require a rewritebase rule, if so, uncomment the RewriteBase line below. If you are running from a subdirectory, your rewritebase should match the name of the path to where stacey is stored.
# ie. if in a folder named 'stacey', RewriteBase /stacey
#RewriteBase /
ErrorDocument 404 /404.html
# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.|\?)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]
# Rewrite any calls to /render to the image parser
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]
# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]
# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]`
提前致谢!
我取得了一些进展。如下:
location / {
if ($uri ~ "/app/$"){
rewrite ^/app/ /index.php last;
}
if (!-e $request_filename){
rewrite ^/(.*)/$ /index.php?$1 last;
}
if (!-f $request_filename) {
rewrite ^/(?!public/)(.+);
}
if (!-f $request_filename) {
rewrite ^/(?!public/)(.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ last;
}
if (!-e $request_filename) {
rewrite ^/(?!public/)(.+)$ public/$1 last;
}
现在重写所有项目没有任何问题。例如;
domain.com/?/projects/project-name-3/ 变成了 domain.com/projects/project-name-3/
但还有另外 2 个页面。其中一个是“关于”,另一个是“联系我”。它们也变成了;domain.com/about 和 domain.com/contact-me,但当我单击它们时,页面会加载,但没有 CSS 样式。有什么线索吗?
好的,新代码是这样的,它给了我错误;nginx:[emerg] /etc/nginx/sites-enabled/domain.com:21 中的“rewrite”指令中的参数数量无效
location / {
rewrite ^/app/ /index.php last;
}
if (!-e $request_filename){
rewrite ^/(.*)/$ /index.php?$1 last;
}
if (!-f $request_filename) {
rewrite ^/(?!public/)(.+);
}
if (!-f $request_filename) {
rewrite ^/(?!public/)(.+)\.(html|json|xml|atom|rss|rdf|txt)$ $2/ last;
}
try_files /public$uri /public$uri/ $uri $uri/ /index.php;
}
21.行的代码是;
rewrite ^/(?!public/)(.+);
答案1
我最近使用以下配置将一些 Stacey 网站迁移到 Nginx:
server {
root SERVER FILE LOCATION;
index index.php index.html index.htm;
# Make site accessible from
server_name YOUR IP OR DOMAIN NAME;
location / {
# Stacey Rules
error_page 404 /404.html;
# Rewrite any calls to /render to the image parser
rewrite ^/render/. /app/parsers/slir/ break;
# Rewrite any file calls to the public directory
try_files $uri $uri/ /index.php?$uri;
}
# Block access to .htaccess files
location ~ /\.ht {
deny all;
}
include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
}
注意:包含文件只是标准错误页面链接和 PHP(Fast-cgi php-fpm)配置,我将它们放在一起以避免在多个站点设置中重复相同的操作。
答案2
你已经基本到达目的地了。
五个中的三个rewrite
可以用 nginx 的更快、更简单的方式替换try_files
指令。一个可以简化一点。第五个有逻辑错误,如您的问题中所述。
首先,您不需要将此检查放在其中,
if
因为它包含其自己的模式匹配。location / { rewrite ^/app/ /index.php last;
这也可以放在它自己的
location
块中,但留待以后再说……第二,你的逻辑错误。当你重写这条规则时,你将两个
()
模式匹配改为三个,但没有调整反向引用$1
以$2
解释其在新正则表达式中的位置变化。if (!-f $request_filename) { rewrite ^/(?!public/)(.+)\.(html|json|xml|atom|rss|rdf|txt)$ $2/ last; }
最后,
try_files
。对于非常简单的匹配,这比 快得多rewrite
,也更容易理解。所以...try_files /public$uri /public$uri/ $uri $uri/ /index.php; }
这应该能让你完成 99% 的任务,除非你根据问题中没有的信息进行最后的调整。
答案3
我正在使用 stacey cms,下面是我用来代替 apache 的 nginx 配置:
server {
listen 80;
server_name localhost;
root /home/vagrant/test;
index index.php index.html index.htm;
error_page 404 /404.html;
location / {
if ($query_string != "") {
return 301 $scheme://$http_host$query_string;
}
try_files $uri $uri/ /index.php?$uri;
}
location ~ \.php$ {
try_files $uri =403;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
if if
block 绝对不是 nginx 配置的理想或惯用方法。但对于与 stacey 合作来说,这是必需的。