简体   繁体   中英

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 ?

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

Doing an automated conversion of Unity's .htaccess using http://winginx.com/en/htaccess yields this:

# 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;
}

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;
}

I hope it will help you. :)

How to do it on 5.5:

1) Go to Release folder and Rename all file extensions ending with .extensiongz to .extension.gz

2) Make sure your files don't have any spaces in them, I always leave mine snake_cased

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

4) Update your sites-available configuration file with:

location ~* {
        gzip_static on;
}

5) Restart nginx with sudo service nginx restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM