Skip to content

Instantly share code, notes, and snippets.

@hieulm
Last active January 13, 2020 19:02
Show Gist options
  • Save hieulm/2307597df50dbdeb59c33950b26382bc to your computer and use it in GitHub Desktop.
Save hieulm/2307597df50dbdeb59c33950b26382bc to your computer and use it in GitHub Desktop.
Nginx Config

Instructions

touch /etc/nginx/site-availables/jenkins.example.com
# Update jenkins.example.com with content in gist
ln -s /etc/nginx/site-availables/jenkins.example.com /etc/nginx/site-enables/jenkins.example.com
nginx -t
systemctl restart nginx
# Config SSL with Let's encrypt & certbot
certbot --nginx -d jenkins.example.com -d www.jenkins.example.com
#
# Wide-open CORS config for nginx
#
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# 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;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
server {
if ($host = jenkins.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
return 301 https://$host$request_uri;
server_name jenkins.example.com;
}
server {
listen 443 ssl;
server_name jenkins.example.com;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-UP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect http:// https://;
proxy_pass http://jenkins;
}
ssl_certificate /etc/letsencrypt/live/jenkins.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/jenkins.example.com/privkey.pem; # managed by Certbot
}
upstream jenkins {
server 127.0.0.1:8080 fail_timeout=0;
}
error_log stderr debug;
server {
if ($host = ws.comspaces.me) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name ws.comspaces.me;
#location / {
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header Host $host;
#
# proxy_pass http://ws;
#
# # enable WebSockets
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection "upgrade";
# }
}
server {
listen 443 ssl;
server_name ws.comspaces.me;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect http:// https://;
proxy_pass http://ws;
}
location /socket.io {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Frappe-Site-Name user-online-service.local;
# proxy_set_header Origin $scheme://$http_host;
proxy_set_header Host $host;
set $cors '';
#if ($http_origin ~ '^https?://(localhost|www\.comspaces.me\|www\.comspaces.me)') {
# set $cors 'true';
#}
#if ($http_origin ~ '^http?://(localhost:3000)') {
# set $cors 'true';
#}
set $cors 'true';
# proxy_set_header 'Access-Control-Allow-Origin' '*';
if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
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' 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;
}
proxy_pass http://ws;
}
ssl_certificate /etc/letsencrypt/live/ws.comspaces.me/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ws.comspaces.me/privkey.pem; # managed by Certbot
}
upstream ws {
server 127.0.0.1:3001 fail_timeout=0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment