How to Setup Nginx Reverse Proxy
Setting up an Nginx reverse proxy involves configuring Nginx to act as an intermediary between clients and your backend web applications.
A reverse proxy intercepts incoming network traffic and forwards it to the appropriate backend server, enhancing security, performance, and reliability.
Nginx excels at this, capable of handling SSL termination, caching, and load balancing. Many applications built with technologies like Node.js or Python benefit from this setup, as they might not include their own robust web server.
For instance, a common configuration sees Nginx listening on port 80 and directing traffic to an application running on port 3000.
Configure Nginx by defining a server block with a listen directive for the port and a location block. Inside the location block, use the proxy_pass directive to specify the backend server’s address. This setup directs incoming requests to your application.
How to use Nginx as a reverse proxy server
You can configure Nginx as a proxy server using the code snippet below. You’ll set up a server block for your domain and specify a location to the backend server where Nginx should send incoming requests.
server {
listen 80;
server_name example.com;
location /backend_app {
proxy_pass http://127.0.0.1:8080;
}
}The configuration above tells Nginx to pass all requests to the /backdend_app location to the proxied server at http://127.0.0.1:8080.
For instance, if a client requests http://example.com/backend_app, Nginx will forward those requests to the backend server defined on the proxy_pass line: http://127.0.0.1:8080.
While that basic configuration shows the fundamentals, Nginx can do much more with proxy servers. For instance, proxy servers can also handle requests for non-HTTP servers like these:
- fastcgi_pass – reverse proxy to a FastCGI server.
- uwsgi_pass – reverse proxy to a uwsgi server.
- scgi_pass – reverse proxy to an SCGI server.
A common Nginx reverse proxy to non-HTTP hosts is done using PHP-FPM. An example is how Nginx serves PHP scripts.
server {
# ... other server block configs
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}How to configure Nginx reverse proxy options
Reverse proxy servers have common options that define how requests are handled and served to the backend servers or apps. These configuration options are recommended when using Nginx as a proxy. In most cases, these headers and parameters will work across all environments where Nginx is deployed as a reverse proxy.
location/ {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
}You can visit the Nginx site for more options to use in your environment.
That should wrap things up!
Conclusion:
Setting up Nginx as a reverse proxy server can significantly enhance the efficiency and performance of your web applications. Here are the key takeaways:
- Flexibility: Nginx can handle both HTTP and non-HTTP requests, making it suitable for various backend applications.
- Efficiency: It offloads the processing from backend servers, managing tasks like SSL encryption and caching.
- Scalability: Load balancing features help distribute client requests efficiently across multiple servers, improving performance.
- Customizability: Nginx’s configuration options allow you to tailor the reverse proxy settings to meet your specific needs.
- Support for Modern Applications: Use Nginx with popular backend technologies like Node.js, Python, and PHP.
By following the basic configurations and using the options discussed, you can effectively implement Nginx as a reverse proxy in your environment.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!