Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve all existing static files directly with NGINX, but proxy the rest to a backend 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 will serve all existing files directly using Nginx (e.g. Nginx just displays PHP source code), otherwise forward a request to Apache. I need to exclude *.php files from the rule so that requests for *.php are also passed to Apache and processed.

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

EDIT: There is white list approach, but it is not very elegant, See all those extensions, I don't 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: On newer versions of Nginx use try_files instead http://wiki.nginx.org/HttpCoreModule#try_files

like image 321
fmalina Avatar asked May 15 '09 14:05

fmalina


People also ask

Can NGINX be used for serving static content?

Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.

How does NGINX reverse proxy work?

A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server. A reverse proxy provides an additional level of abstraction and control to ensure the smooth flow of network traffic between clients and servers.

Can NGINX be used as proxy server?

NGINX was initially designed as a reverse proxy server. However, with continuous development, NGINX also serves as one of the options to implement the forward proxy. The forward proxy itself is not complex, the key issue it addresses is how to encrypt HTTPS traffic.


2 Answers

Use try_files and named location block ('@apachesite'). This will remove unnecessary regex match and if 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: The assumption of this config is that there doesn't exist any php script under /path/to/root/of/static/files. This is common in most modern php frameworks. In case your legacy php projects have both php scripts and static files mixed in the same folder, you may have to whitelist all of the file types you want nginx to serve.

like image 164
Chuan Ma Avatar answered Oct 12 '22 01:10

Chuan Ma


Try this:

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

Hopefully it works. Regular expressions have higher priority than plain strings, so all requests ending in .php should be forwared to Apache if only a corresponding .php file exists. Rest will be handled as static files. The actual algorithm of evaluating location is here.

like image 45
Jasiu Avatar answered Oct 12 '22 02:10

Jasiu