How to convert this apache rewrite to nginx?

Listed below are apache rewrite rules for webgl unity

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

  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 this will not work, based on my understanding of the original rewrite of apache, it assumes js | data | mem | unity3d in gunzip format plus adding the name "gz" at the end of the file name, how to rewrite it in nginx while gzip_static does not support a custom file name?

+5
source share
4 answers

You may already have understood this, but I came across this question when I was struggling with the conversion of Apache to Nginx.

Nginx has a gzip_static module that automatically searches for pre-compressed versions of files and maintains them if the client supports gzip. To do this, move the 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 configuration:

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

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

 nginx -V 

The only drawback is that you need to move / rename files, but this is a slight inconvenience for a single-layer solution.

Here's how to move / rename gzip files as expected by nginx:

 cd Data mv ../Compressed/*gz ./ rename 's/(.*)gz$/$1.gz/' *gz 
+9
source

Performing an automatic Unity.htaccess conversion using http://winginx.com/en/htaccess yields the following:

 # nginx configuration location / { rewrite (.*)Data(.*)\.js /$1Compressed$2\.jsgz break; rewrite (.*)Data(.*)\.data /$1Compressed$2\.datagz break; rewrite (.*)Data(.*)\.mem /$1Compressed$2\.memgz break; rewrite (.*)Data(.*)\.unity3d /$1Compressed$2\.unity3dgz break; } 
0
source

Here is my solution. I tested it on nginx / 1.2.1.

 location ~* ^/new/build/webgl_build/Release/.+\.(js|data|mem|unity3d) { add_header Content-Encoding gzip; gzip_static on; rewrite (.*)Release(.*)\.js $1Compressed$2.jsgz break; rewrite (.*)Release(.*)\.data $1Compressed$2.datagz break; rewrite (.*)Release(.*)\.mem $1Compressed$2.memgz break; rewrite (.*)Release(.*)\.unity3d $1Compressed$2.unity3dgz break; } 

Hope this helps you. :)

0
source

How to do it on 5.5:

1) Go to the Release folder and rename all file extensions ending in .extensiongz to .extension.gz

2) Make sure there are no spaces in your files, I always leave my snake_cased

3) Make sure you have static gzip installed with nginx by running nginx -V

4) Update the configuration file available for your sites using

 location ~* { gzip_static on; } 

5) Restart nginx with sudo service nginx restart

0
source

Source: https://habr.com/ru/post/1209591/


All Articles