Created
September 2, 2025 13:44
-
-
Save onequbit/04a28a0776a0d726800250b6b62ce551 to your computer and use it in GitHub Desktop.
example nginx config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This file routes requests for app1.example.com, app2.example.com, and app3.example.com | |
| # to their corresponding backend services. | |
| # | |
| # Each application has two components: | |
| # 1. A web server for the user interface (e.g., a Node.js/Express app serving HTML/CSS/JS). | |
| # 2. A REST API server for data operations. | |
| # Defines settings for HTTP traffic. | |
| http { | |
| # Upstream groups define pools of backend servers. This makes the configuration | |
| # cleaner and allows for load balancing if you add more servers. | |
| # --- Upstream definitions for Application 1 --- | |
| upstream app1_web { | |
| # Server(s) for the browser-based UI of App 1 | |
| server 127.0.0.1:3001; | |
| } | |
| upstream app1_api { | |
| # Server(s) for the REST API of App 1 | |
| server 127.0.0.1:8001; | |
| } | |
| # --- Upstream definitions for Application 2 --- | |
| upstream app2_web { | |
| # Server(s) for the browser-based UI of App 2 | |
| server 127.0.0.1:3002; | |
| } | |
| upstream app2_api { | |
| # Server(s) for the REST API of App 2 | |
| server 127.0.0.1:8002; | |
| } | |
| # --- Upstream definitions for Application 3 --- | |
| upstream app3_web { | |
| # Server(s) for the browser-based UI of App 3 | |
| server 127.0.0.1:3003; | |
| } | |
| upstream app3_api { | |
| # Server(s) for the REST API of App 3 | |
| server 127.0.0.1:8003; | |
| } | |
| # Set common proxy headers in one place to avoid repetition. | |
| # These headers pass useful client information to the backend servers. | |
| 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; | |
| # ================================================================= | |
| # Server Block for app1.example.com | |
| # ================================================================= | |
| server { | |
| listen 80; | |
| server_name app1.example.com; | |
| # Location for the browser-based endpoint (e.g., the main website) | |
| location / { | |
| proxy_pass http://app1_web; | |
| # WebSockets support | |
| proxy_http_version 1.1; | |
| proxy_set_header Upgrade $http_upgrade; | |
| proxy_set_header Connection 'upgrade'; | |
| } | |
| # Location for the REST API endpoint | |
| # This matches any request starting with /api/ | |
| location /api/ { | |
| # proxy_pass will forward the request to the API server. | |
| # The URI part (/api/) is passed along as well. | |
| proxy_pass http://app1_api; | |
| } | |
| } | |
| # ================================================================= | |
| # Server Block for app2.example.com | |
| # ================================================================= | |
| server { | |
| listen 80; | |
| server_name app2.example.com; | |
| location / { | |
| proxy_pass http://app2_web; | |
| proxy_http_version 1.1; | |
| proxy_set_header Upgrade $http_upgrade; | |
| proxy_set_header Connection 'upgrade'; | |
| } | |
| location /api/ { | |
| proxy_pass http://app2_api; | |
| } | |
| } | |
| # ================================================================= | |
| # Server Block for app3.example.com | |
| # ================================================================= | |
| server { | |
| listen 80; | |
| server_name app3.example.com; | |
| location / { | |
| proxy_pass http://app3_web; | |
| proxy_http_version 1.1; | |
| proxy_set_header Upgrade $http_upgrade; | |
| proxy_set_header Connection 'upgrade'; | |
| } | |
| # Example of rewriting the URL before proxying. | |
| # This can be useful if your API server doesn't expect the /api prefix. | |
| location /api/ { | |
| rewrite /api/(.*) /$1 break; # Removes /api/ from the request URI | |
| proxy_pass http://app3_api; | |
| } | |
| } | |
| # A default server to catch requests that don't match any other server_name | |
| server { | |
| listen 80 default_server; | |
| server_name _; # Underscore is a catch-all server name | |
| location / { | |
| return 404; # Or return a specific error page | |
| } | |
| } | |
| } | |
| # The 'events' block is required but can be empty for basic configurations. | |
| events { | |
| worker_connections 1024; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment