Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this apache rewrite into nginx?

The following are apache rewrite rules for unity webgl

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteRule (.*)Data(.*)\.js $1Compressed$2\.jsgz [L]
RewriteRule (.*)Data(.*)\.data $1Compressed$2\.datagz [L]
RewriteRule (.*)Data(.*)\.mem $1Compressed$2\.memgz [L]
RewriteRule (.*)Data(.*)\.unity3d $1Compressed$2\.unity3dgz [L]
AddEncoding gzip .jsgz
AddEncoding gzip .datagz
AddEncoding gzip .memgz
AddEncoding gzip .unity3dgz

Currently on nginx i have the following

   location ~* \.(js|data|mem|unity3d)$
   {
   gzip_static on;

   if ($request_filename ~ "\.jsgz$" ) {
                rewrite ^(.js.gz)$ /$1.jsgz;
   }
   if ($request_filename ~ "\.datagz$" ) {
                rewrite ^(.data.gz)$ /$1.datagz;
   }
   if ($request_filename ~ "\.memgz$" ) {
                rewrite ^(.mem.gz)$ /$1.memgz;
   }
   if ($request_filename ~ "\.unity3dgz$" ) {
                rewrite ^(.unity3d.gz)$ /$1.unity3dgz;
   }
   }



   location ~* \.(jsgz|datagz|memgz|unity3dgz)$ {
   add_header Content-Encoding gzip;
   break;
   }

But it doesn't work, based on my understanding of the original apache rewrite, it suppose to automatically compressed js|data|mem|unity3d into gunzip format plus adding "gz" name in the end of filename, how to rewrite in nginx while gzip_static doesn't support custom filename ?

like image 487
Prodigy Avatar asked Dec 20 '14 06:12

Prodigy


People also ask

What does rewrite to nginx?

NGINX rewrite rules are used to change entire or a part of the URL requested by a client. The main motive for changing an URL is to inform the clients that the resources they are looking for have changed its location apart from controlling the flow of executing pages in NGINX.

What is .htaccess equivalent for nginx?

Since Nginx does not have an equivalent to the . htaccess file (i.e. no directory level configuration files), you need to update the main configuration and reload nginx for any changes to take effect.

How do I rewrite an Apache module?

In order for Apache to understand rewrite rules, we first need to activate mod_rewrite . It's already installed, but it's disabled on a default Apache installation. Use the a2enmod command to enable the module: sudo a2enmod rewrite.


1 Answers

You may have figured this out by now, but I stumbled on this question when struggling with the Apache to Nginx conversion myself.

Nginx has a module called gzip_static which will automatically look for pre-compressed versions of files and serve them if the client supports gzip. This requires that you move files from the Compressed directory to the Data directory, and slightly change the file extensions:

Before:

# cd MyProject
# tree
.
|-- Compressed
|   |-- MyProject.datagz
|   |-- MyProject.html.memgz
|   |-- MyProject.jsgz
|   |-- fileloader.jsgz
|   |-- UnityConfig.jsgz
|   |-- UnityProgress.jsgz
|-- Data
|   |-- MyProject.data
|   |-- MyProject.html.mem
|   |-- MyProject.js
|   |-- fileloader.js
|   |-- UnityConfig.js
|   |-- UnityProgress.js

After:

# tree
.
|-- Compressed
|-- Data
|   |-- MyProject.data
|   |-- MyProject.data.gz
|   |-- MyProject.html.mem
|   |-- MyProject.html.mem.gz
|   |-- MyProject.js
|   |-- MyProject.js.gz
|   |-- fileloader.js
|   |-- fileloader.js.gz
|   |-- UnityConfig.js
|   |-- UnityConfig.js.gz
|   |-- UnityProgress.js
|   |-- UnityProgress.js.gz

In your Nginx config:

location /path/to/MyProject/Data {
    gzip_static on;
}

Note that Nginx must be built with --with-http_gzip_static_module. You can check to see if your version has this already by running:

nginx -V

The only drawback is that you have to move/rename the files, but that is a minor inconvenience for a one-liner solution.

Here's how to move/rename the gzip files to match what nginx expects:

cd Data
mv ../Compressed/*gz ./
rename 's/(.*)gz$/$1.gz/' *gz
like image 155
Jody Avatar answered Oct 17 '22 11:10

Jody