在nginx服务器上托pipeStacey? (将.htaccess转换为nginx)

Stacey是一个轻量级的内容pipe理系统。 我在我的porfolio网站上使用它,但是它使用.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:“rewrite”指令中无效的参数个数:21

 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/)(.+); 

我最近使用这种configuration将一些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)configuration我已经放在一起保存在多个站点设置中重复相同的事情。

通过https://github.com/kolber/stacey/issues/143

你大部分是在那里。

五个rewrite的三个可以用nginx更快更简单的try_files指令replace。 一个可以简化一下。 第五个逻辑错误发布在你的问题。

  1. 首先,你不需要把这个检查放在一个if因为它包含了它自己的模式匹配。

     location / { rewrite ^/app/ /index.php last; 

    这也可以放在自己的location块,但稍后离开…

  2. 其次,你的逻辑错误。 当你改写这个规则时,你将两个()模式匹配改为三,但是没有将反向引用$1调整到$2以考虑它在新正则expression式中改变的位置。

      if (!-f $request_filename) { rewrite ^/(?!public/)(.+)\.(html|json|xml|atom|rss|rdf|txt)$ $2/ last; } 
  3. 最后, try_files 。 对于非常简单的匹配,这比重rewrite要快得多,也更容易理解。 所以…

      try_files /public$uri /public$uri/ $uri $uri/ /index.php; } 

这应该让你有99%的方式,除了基于信息不是你的问题的任何最后的调整。

我正在使用stacey cms,这里是我用于nginxconfiguration的Apache的地方:

 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; } } 

如果块是肯定不理想或与nginxconfiguration惯用。 在这里工作与stacey tho必要的。