I am trying to serve static HTML with server side PHP processing with Nginx. I have no experience with Apache. I would like to migrate the following re-writes from Apache's .htaccess to Nginx. What would be the corresponding rewrites for a server or location block in Nginx's default.conf?

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

php_flag zlib.output_compression On

My current default.conf looks like this:

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;

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 {

I have spent the day reading the documentation at Nginx - http://wiki.nginx.org/NginxHttpRewriteModule and the documentation at Apache - http://httpd.apache.org/docs/current/mod/mod_rewrite.html but it has only culminated into horror and despair. I seek your help :)

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]

would translate to:

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.