我试图将rewrite rules从.htaccess文件转换为nginxconfiguration。 作为移动网站到NGINXnetworking服务器的一部分。
这里是从.htaccess文件重写规则
Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_URI} !\.php$ RewriteCond %{REQUEST_URI} !\.gif$ RewriteCond %{REQUEST_URI} !\.jp?g$ RewriteCond %{REQUEST_URI} !\.png$ RewriteCond %{REQUEST_URI} !\.css$ RewriteCond %{REQUEST_URI} !\.js$ RewriteCond %{REQUEST_URI} !\.html$ RewriteCond %{REQUEST_URI} !\.xhtml$ RewriteCond %{REQUEST_URI} !\.htm$ RewriteCond %{REQUEST_URI} !\.ico$ RewriteRule (.*) index.php [L] RewriteRule (.*).html index.php [L]
我当前的nginx config 。
server { listen 80; server_name example.com; root /usr/share/nginx/html; location / { index index.php index.html index.htm; } location ~ \.(php)$ { try_files $uri =404; root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location !~* .(css|js|html|htm|xhtml|php|png|gif|ico|jpg|jpeg)$ { # Matches requests ending in png, gif, ico, jpg or jpeg. rewrite ^(.*)$ /index.php break; rewrite (.*).html /index.php break; } }
主页正在工作,但内部网站链接正在给404。
到目前为止,我已经尝试了许多像这样的重写规则。
if ( $uri ~ ^/(?!(\.css|\.js|\.html|\.htm|\.xhtml|\.php|\.png|\.gif|\.ico|\.jpg|\.jpeg))) { rewrite ^/(.*)$ /index.php break; }
但有时,内部链接工作,同时redirect所有静态文件,如.css,.js到主页,有时我不能访问/administrator/我的网站,它redirect到主页。
我如何复制.htaccess重写规则到nginx ?
对我来说,这看起来像一个标准的前端控制器模式。 在nginx中,它是这样实现的:
server { listen 80; server_name example.com; root /usr/share/nginx/html; location / { try_files $uri $uri/ /index.php; } location ~ \.(php)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
这里的逻辑是,它首先尝试查找请求的资源是否作为文件或目录存在。 如果存在,则返回给客户端。 否则,请求被传递给PHP脚本,然后PHP脚本确定要返回给客户端的内容。