Advertisement
Kenya-West

A snippet for https://t.me/nginx_ru #3

Oct 18th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nginx 2.26 KB | Source Code | 0 0
  1. events {}
  2.  
  3. error_log /var/log/nginx/error.log debug;
  4.  
  5. http {
  6.     # Add CORS headers for all requests
  7.     add_header 'Access-Control-Allow-Origin' '*' always;
  8.     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  9.     add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
  10.  
  11.     map $http_x_proxy_port $xproxyport {
  12.         default 80;  # Default port if X-PROXY-PORT header is not set
  13.         ~^[0-9]+$ $http_x_proxy_port;  # Use the value of X-PROXY-PORT if it is a number
  14.     }
  15.  
  16.     log_format custom_debug 'Host: $host, Container: $container, Port: $xproxyport, Path: $uri, X-PROXY-PORT: $http_x_proxy_port, complete proxying URL will be: http://$container:$xproxyport$rest_of_path';
  17.     access_log /var/log/nginx/access.log custom_debug;
  18.  
  19.     server {
  20.  
  21.         listen 80;
  22.         http2 on;
  23.         server_name proxy;
  24.  
  25.         resolver 127.0.0.11;
  26.  
  27.         location / {
  28.             root /usr/share/nginx/html;
  29.             index index.html;
  30.  
  31.             proxy_set_header Host $host;
  32.             proxy_set_header X-Real-IP $proxy_protocol_addr;
  33.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  34.             proxy_set_header X-Forwarded-Proto $scheme;
  35.  
  36.             if ($request_method = OPTIONS) {
  37.                 add_header 'Content-Type' 'text/plain; charset=utf-8';
  38.                 add_header 'Content-Length' 0;
  39.                 return 204;
  40.             }
  41.         }
  42.  
  43.         location ~ ^/proxy/([^/]+)(/.*)?$ {
  44.             # Extract container name from the URL
  45.             set $container $1;
  46.             set $rest_of_path $2;
  47.  
  48.  
  49.             if ($request_method = OPTIONS) {
  50.                 add_header 'Content-Type' 'text/plain; charset=utf-8';
  51.                 add_header 'Content-Length' 0;
  52.                 return 204;
  53.             }
  54.  
  55.             # Proxy the request to the respective container and port
  56.             proxy_pass http://$container:$xproxyport$rest_of_path;
  57.             proxy_set_header Host "0.0.0.0";
  58.             proxy_set_header X-Real-IP $remote_addr;
  59.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  60.             proxy_set_header X-Forwarded-Proto $scheme;
  61.         }
  62.     }
  63. }
  64.  
Tags: nginx
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement