Advertisement
SimonKuzmichev

Nginx help

Sep 25th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nginx 1.88 KB | None | 0 0
  1. worker_processes auto;
  2.  
  3. events {
  4.     worker_connections  1024;
  5. }
  6.  
  7. http {
  8.     include       mime.types;
  9.  
  10.     log_format alogformat "$time_local [location: $location_path][http_origin: $http_origin][cors: $cors] $scheme://$host:$server_port$request_uri";
  11.  
  12.     error_log  /var/log/nginx/error.log debug;
  13.     access_log /var/log/nginx/access.log alogformat;
  14.  
  15.     ssl_certificate     /etc/letsencrypt/live/example.com-0001/fullchain.pem;
  16.     ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem;
  17.  
  18.     map $http_origin $cors {
  19.         default "test";
  20.         ~^https?://([a-z0-9]+[.])*example\.com(:[0-9]+)?/?$ "$http_origin";
  21.         ~^http://localhost:(3000|8080)?/?$ "$http_origin";
  22.     }
  23.  
  24.     server {
  25.         server_name api.example.com;
  26.         listen 443 ssl;
  27.  
  28.         location /api/ {
  29.             set $location_path 'api';
  30.  
  31.             add_header 'Access-Control-Allow-Origin' $cors always;
  32.             add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PATCH, PUT, HEAD';
  33.             add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,Accept';
  34.             add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
  35.             add_header 'Access-Control-Allow-Credentials' 'true';
  36.  
  37.             if ($request_method = 'OPTIONS') {
  38.                 add_header 'Access-Control-Max-Age' 1728000;
  39.                 add_header 'Content-Type' 'text/plain charset=UTF-8';
  40.                 add_header 'Content-Length' 0;
  41.  
  42.                 return 204;
  43.             }
  44.  
  45.             rewrite ^/api/(.*)$ /$1 break;
  46.  
  47.             proxy_set_header X-Real-IP         $remote_addr;
  48.             proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
  49.             proxy_set_header X-Forwarded-Proto $scheme;
  50.             proxy_set_header X-Forwarded-Host  $host;
  51.             proxy_set_header X-Forwarded-Port  $server_port;
  52.  
  53.             proxy_pass http://apiapp:8080;
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement