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?
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.
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With