A reverse proxy acts as an intermediary server between clients and backend servers, forwarding requests and responses. Using NGINX as a reverse proxy enhances performance, strengthens security, simplifies application routing, and facilitates session sharing.
Refer to the following posts to understand more about the reverse proxy - Apache Reverse Proxy: Content From Different Websites | by Albin Issac | The Startup | Medium
Steps to Set Up NGINX as a Reverse Proxy on Windows:
Install NGINX:
- Download NGINX for Windows from the official website -nginx: download
- Extract the
.zip
file to a folder, e.g.,C:\nginx
- Start NGINX
start nginx
Configure Reverse Proxy:
- Open the
nginx.conf
file inC:\nginx\conf\nginx.conf
Add the reverse proxy configuration: Here is the minimal configuration to proxy the requests to the backend server. You can add multiple servers and locations.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format upstream_logging '[$time_local] $remote_addr - $remote_user - $server_name '
'to: $upstream_addr: $request '
'status $status upstream_response_time $upstream_response_time '
'request_time $request_time '
'headers: "$http_user_agent,
$http_x_forwarded_for, $http_x_cip_client_id" '
'upstream_headers: "$upstream_http_content_type,
$upstream_http_content_length"';
sendfile on;
keepalive_timeout 65;
server {
listen 8000;
listen [::]:8000;
#SSL Support
#listen 8000 ssl;
#listen [::]:8000 ssl;
server_name localhost;
#Debug Logging
#error_log logs/error.log debug;
access_log logs/api_logging.log upstream_logging;
#Configurations to support SSL
#ssl_certificate C://cert/localhost.crt;
#ssl_certificate_key C://cert/localhost.key;
#ssl_session_cache shared:SSL:1m;
#ssl_session_timeout 5m;
#ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_prefer_server_ciphers on;
proxy_buffering off;
proxy_busy_buffers_size 512k;
proxy_buffers 4 512k;
proxy_buffer_size 256k;
location / {
root html;
index index.html index.htm;
}
location /test/api {
proxy_pass https://api.server.com;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Header1 Header1 value;
proxy_set_header Header2 Header2 value;
proxy_ssl_name $proxy_host;
proxy_ssl_server_name on;
#Set the Response Header with backend response headers for debugging
#add_header X-Backend-Status $upstream_status;
#add_header X-Backend-Server $upstream_addr;
#add_header X-Backend-Content-Type $upstream_http_content_type;
#add_header X-Backend-Content-Length $upstream_http_content_length;
proxy_connect_timeout 60s; # Timeout for connecting to the upstream
proxy_send_timeout 60s; # Timeout for sending to the upstream
proxy_read_timeout 60s; # Timeout for reading from the upstream
}
}
}
nginx -s reload
Test the Setup:
http://localhost:8000/test/api/getOrder
in your browser. It should forward requests to your backend server and return the response.
nginx -s stop
With a few simple steps, you can enable features like load balancing, SSL termination, and caching, making your application more robust and scalable.
No comments:
Post a Comment