Sometimes we have to set up nginx behind a proxy either because we can’t have multiple public ports open, or because we have complex micro service apps powering our application or simply to enable HTTPS.
In this configuration I’m not going to show how to enable HTTPS on your server.
I just want to show you 3 different ways to reverse proxy.
# # # This configuration shows multiple configurations possible to reverse proxy stuff. # worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { listen 8081; listen [::]:8081 ipv6only=on; server_name localhost; # # Example 0) # A request received at nginx_host:8081/ws1/foo.png # will be sent to the server as follow 192.168.0.195:8082/ws1/foo.png # location /ws1/ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP2 $realip_remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_set_header X-Htz-Proxy true; # this is a custom header proxy_read_timeout 5m; proxy_connect_timeout 5m; proxy_pass http://192.168.0.195:8082; proxy_redirect off; } # # Example 1) The most basic example. # The nginx root matches our server root # # A request received at nginx_host:8081/foo.png will be sent to server # as follows: http://localhost:8083/foo.png # location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP2 $realip_remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_set_header X-Htz-Proxy true; # this is a custom header proxy_read_timeout 5m; proxy_connect_timeout 5m; proxy_pass http://localhost:8083; proxy_redirect off; } # # Example 2) Advanced # A request received at nginx_host:8081/ws2/foo.png will be sent to server # as follows: http://localhost:8083/foo.png # #location ~ /ws2/ { # rewrite /ws2(?:/)?(.*) /$1 break; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Real-IP2 $realip_remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # proxy_set_header X-Forwarded-Proto $scheme; # proxy_set_header Host $http_host; # proxy_set_header X-NginX-Proxy true; # proxy_set_header X-Htz-Proxy true; # this is a custom header # proxy_read_timeout 5m; # proxy_connect_timeout 5m; # proxy_pass http://localhost:8083; # proxy_redirect off; #} error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }