我正在尝试使用 Nginx 提供带有服务器端 PHP 处理的静态 HTML。我没有使用 Apache 的经验。我想将以下重写从 Apache 的 .htaccess 迁移到 Nginx。Nginx 的 default.conf 中服务器或位置块的相应重写是什么?
#.htaccess
AddDefaultCharset utf-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(min\-merge\-cache)=(css|js)\/(.*)$ /static-server/min-merge-cache.php? files=$3&format=$2 [NC,L,NS]
RewriteRule ^$ /static-server/file-server.php?file=index&ext=html [NC,L,NS]
RewriteRule ^(.*)\.(js|css|html|htm)$ /static-server/file-server.php?file=$1&ext=$2 [NC,L,NS]
RewriteRule ^(images)\/(.*)\.(jpe?g|gif|ico|png|swf|flv)$ /static-server/file- server.php?file=$1/$2&ext=$3&img=1 [NC,L,NS]
FileETag None
</IfModule>
php_flag zlib.output_compression On
我当前的 default.conf 如下所示:
server {
listen 8080;
port_in_redirect off;
add_header Cache-Control public;
expires max;
server_name www.mydomain.com;
root /usr/share/nginx/www;
index index.html index.php;
#Rewrites
rewrite ^(min\-merge\-cache)=(css|js)\/(.*)$ /static-server/min-merge-cache.php?files=$3&format=$2;
location = /favicon.ico {
return 204;
}
location = /index.html {
return 301 http://www.mydomain.com/;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass localhost:9000;
}
#Client Cache
gzip on;
gzip_types text/css application/x-javascript text/x-component text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ \.(css|js|htc)$ {
expires 1y;
}
location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
expires 1y;
}
location ~ \. (asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|m id|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png |pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw |zip)$ {
expires 1y;
}
## All errors get the generic error page
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 495 496 497
500 501 502 503 504 505 506 507 /error_page.html;
location /404.html {
internal;
}
}
我花了一天时间阅读 Nginx 的文档 -http://wiki.nginx.org/NginxHttpRewriteModule以及 Apache 的文档 -http://httpd.apache.org/docs/current/mod/mod_rewrite.html但它只会带来恐惧和绝望。我寻求你的帮助:)
答案1
RewriteRule ^(min\-merge\-cache)=(css|js)\/(.*)$ /static-server/min-merge-cache.php?files=$3&format=$2 [NC,L,NS]
RewriteRule ^$ /static-server/file-server.php?file=index&ext=html [NC,L,NS]
RewriteRule ^(.*)\.(js|css|html|htm)$ /static-server/file-server.php?file=$1&ext=$2 [NC,L,NS]
RewriteRule ^(images)\/(.*)\.(jpe?g|gif|ico|png|swf|flv)$ /static-server/file-server.php?file=$1/$2&ext=$3&img=1 [NC,L,NS]
翻译过来就是:
rewrite ^(min\-merge\-cache)=(css|js)\/(.*)$ /static-server/min-merge-cache.php?files=$3&format=$2 last;
rewrite ^$ /static-server/file-server.php?file=index&ext=html last;
rewrite ^(.*)\.(js|css|html|htm)$ /static-server/file-server.php?file=$1&ext=$2 last;
rewrite ^(images)\/(.*)\.jpe?g|gif|ico|png|swf|flv)$ /static-server/file-server.php?file=$1/$2&ext=$3&img=1 last;