How to serve all existing static files directly using NGINX, but the proxy server remains on the server.

location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; if (-f $request_filename) { access_log off; expires 30d; break; } if (!-f $request_filename) { proxy_pass http://127.0.0.1:8080; # backend server listening break; } } 

Above, it will serve all existing files directly using Nginx (for example, Nginx just displays the PHP source code), otherwise it redirects the request to Apache. I need to exclude * .php files from the rule so that requests for * .php are also transferred to Apache and processed.

I want Nginx to process all static files and Apache to process all dynamic materials.

EDIT: There is a whitelist, but it is not very elegant, see all these extensions, I do not want this.

 location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ { access_log off; expires 30d; } location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; } 

EDIT 2: In newer versions of Nginx use try_files instead of http://wiki.nginx.org/HttpCoreModule#try_files

+64
reverse-proxy nginx static-files
May 15, '09 at 14:24
source share
3 answers

Use try_files and a named location block ('@apachesite'). This will remove the unnecessary regular expression match and block. More efficient.

 location / { root /path/to/root/of/static/files; try_files $uri $uri/ @apachesite; expires max; access_log off; } location @apachesite { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; } 

Update: It is assumed that in this case there is no php script under /path/to/root/of/static/files . This is common in most modern php frameworks. If your legacy php projects have both php scripts and static files mixed in one folder, you may need to whitelist all the types of files you want to use nginx.

+124
Mar 17 '13 at 23:27
source share

Try the following:

 location / { root /path/to/root; expires 30d; access_log off; } location ~* ^.*\.php$ { if (!-f $request_filename) { return 404; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; } 

Hope this works. Regular expressions take precedence over simple lines, so all requests ending in .php should be turned to Apache if only the corresponding .php file exists. Rest will be processed as static files. The actual location estimation algorithm is here .

+15
May 16 '09 at 21:02
source share

If you use mod_rewrite to hide the extension of your scripts, or if you like beautiful URLs that end with /, then you can approach this from a different direction. Tell nginx so that something with a non-static extension goes into apache. For example:

 location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ { root /path/to/static-content; } location ~* ^!.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ { if (!-f $request_filename) { return 404; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; } 

I found the first part of this snippet above: http://code.google.com/p/scalr/wiki/NginxStatic

+5
Feb 13 '12 at 19:37
source share



All Articles