|
# nginx rate limiter https://www.nginx.com/blog/rate-limiting-nginx/ |
|
limit_req_zone $binary_remote_addr zone=api:10m rate=2r/s; |
|
|
|
|
|
# private localhost server |
|
server { |
|
# passenger_friendly_error_pages on; |
|
listen 81; |
|
listen [::]:81; |
|
root /var/www/api2/public; |
|
server_name _; |
|
allow 127.0.0.1; |
|
allow ::1; |
|
deny all; |
|
passenger_app_env production; |
|
passenger_app_group_name "api2 - Wersja API w Node (TypeORM)"; |
|
passenger_group www-data; |
|
|
|
|
|
passenger_nodejs /home/node14/.nvm/versions/node/v14.16.1/bin/node; |
|
passenger_user node14; |
|
|
|
error_log /var/log/nginx/api2.localhost.error.log; |
|
access_log /var/log/nginx/api2.localhost.access.log; |
|
|
|
|
|
passenger_sticky_sessions on; |
|
passenger_enabled on; |
|
passenger_app_type node; |
|
passenger_startup_file /var/www/api2.shinden.pl/app.js; |
|
passenger_force_max_concurrent_requests_per_process 20; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
server { |
|
listen 80; |
|
listen [::]:80; |
|
|
|
listen 443 ssl http2; |
|
listen [::]:443; |
|
include snippets/snakeoil.conf; |
|
ssl_protocols TLSv1.2; |
|
|
|
root /var/www/example.com/build; |
|
server_name example.com; |
|
|
|
access_log /var/log/nginx/example.com.access.log; |
|
|
|
# passenger_friendly_error_pages on; # see what you wrong configure |
|
|
|
# serve static diles through nginx. (It's example for `next`) |
|
location _next/static { |
|
alias /var/www/example.com/build/static; |
|
expires 30d; |
|
access_log on; |
|
} |
|
|
|
|
|
set $cors 0; |
|
if ($http_origin ~ '^https?://(localhost(:[35]000)|([a-z\d]+\.)?example\.(pl|com|org))') { |
|
set $cors 1; |
|
} |
|
if ($cors) { |
|
add_header 'Access-Control-Allow-Origin' "$http_origin" always; |
|
add_header 'Access-Control-Allow-Credentials' 'true' always; |
|
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; |
|
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,Cookie' always; |
|
# required to be able to read Authorization header in frontend |
|
#add_header 'Access-Control-Expose-Headers' 'Authorization' always; |
|
} |
|
|
|
if ($request_method = 'OPTIONS') { |
|
# Tell client that this pre-flight info is valid for 20 days |
|
add_header 'Access-Control-Max-Age' 1728000; |
|
add_header 'Content-Type' 'text/plain charset=UTF-8'; |
|
add_header 'Content-Length' 0; |
|
return 204; |
|
} |
|
|
|
|
|
|
|
# connect another app to route |
|
location /api2/ { |
|
limit_req zone=api; |
|
proxy_pass http://localhost:83/; |
|
proxy_http_version 1.1; |
|
proxy_set_header Upgrade $http_upgrade; |
|
proxy_set_header Connection keep-alive; |
|
proxy_set_header Host $host; |
|
proxy_cache_bypass $http_upgrade; |
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
|
proxy_set_header X-Forwarded-Proto $scheme; |
|
} |
|
|
|
|
|
# use this backend istead main app. see Location priority https://stackoverflow.com/questions/5238377/nginx-location-priority |
|
location ~ ^/api/v2/(.+)$ { |
|
proxy_pass http://localhost:81/$1$is_args$args; |
|
} |
|
|
|
|
|
passenger_app_env production; |
|
passenger_app_group_name "example.com (app)"; # unique name |
|
passenger_group www-data; # www user group |
|
|
|
# I created user for each node version and install `node` through `nvm` |
|
passenger_user node10; |
|
passenger_nodejs /home/node10/.nvm/versions/node/v10.24.1/bin/node; |
|
|
|
passenger_sticky_sessions on; |
|
passenger_enabled on; |
|
|
|
passenger_app_type node; |
|
passenger_startup_file /var/www/example.com/build/index.js; |
|
|
|
passenger_force_max_concurrent_requests_per_process 20; |
|
|
|
} |