Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you serve static files from an nginx server acting as a reverse proxy for a nodejs server?

Tags:

My current nginx config is this:

upstream nodejs {     server 127.0.0.1:3000; }  server {     listen 8080;     server_name localhost;     root ~/workspace/test/app;     index index.html;      location / {         proxy_pass http://nodejs;         proxy_set_header Host $host ;          proxy_set_header X-Real-IP $remote_addr;          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;     } } 

I'm very very new to nginx, but at the very least I know that nginx is better than node/express at serving static files. How can I configure the server so that nginx serves the static files?

like image 563
m0meni Avatar asked Apr 01 '15 05:04

m0meni


People also ask

Can nginx be used as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

How does nginx serve static files?

Root Directory and Index Files mp3 or . mp4 extension, NGINX instead searches for the file in the /www/media/ directory because it is defined in the matching location block. You can list more than one filename in the index directive. NGINX searches for files in the specified order and returns the first one it finds.

How does nginx work as a reverse proxy?

Nginx reverse proxy acts as an intermediate server that intercepts client requests and forwards them to the appropriate upstream backend server and subsequently forwarded a response from the server back to the client. The reverse proxy provides various benefits as an abstract layer above upstream servers.

Is nginx a proxy or reverse proxy?

NGINX Plus and NGINX are the best-in-class reverse proxy and load balancing solutions used by high-traffic websites such as Dropbox, Netflix, and Zynga.


2 Answers

I solved it using this new configuration:

upstream nodejs {     server localhost:3000; }  server {     listen 8080;     server_name localhost;     root ~/workspace/test/app;      location / {         try_files $uri @nodejs;     }      location @nodejs {         proxy_redirect off;         proxy_http_version 1.1;         proxy_pass http://nodejs;         proxy_set_header Host $host ;          proxy_set_header X-Real-IP $remote_addr;          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;     } } 

Thanks to the following Stack Overflow post:


How to serve all existing static files directly with NGINX, but proxy the rest to a backend server.

like image 55
m0meni Avatar answered Nov 10 '22 00:11

m0meni


You'll probably want another location block within your server for the static files.

location /static {   alias /path/to/static/files; } 

This uses the alias directive. Then you can hit files at localhost:8080/static/some_file.css

P.S. You don't need the root or index that you have set currently. (root is similar to alias with a slight difference in usage)

like image 30
Sanketh Katta Avatar answered Nov 10 '22 01:11

Sanketh Katta