Skip to content

Instantly share code, notes, and snippets.

@calebwoods
Created May 10, 2014 20:18
Show Gist options
  • Save calebwoods/5e88b5e323d55ad71195 to your computer and use it in GitHub Desktop.
Save calebwoods/5e88b5e323d55ad71195 to your computer and use it in GitHub Desktop.
Sample Nginx config for deployment of Angular.js app
server { listen 80;
server_name example.com;
access_log /var/log/example.com/nginx.access.log;
error_log /var/log/example.com/nginx.error.log;
root /var/www/apps/example.com/public;
charset utf-8;
location / {
rewrite ^ https://$host$request_uri? permanent;
}
}
server {
listen 443 ssl;
server_name example.com;
access_log /var/log/example.com/nginx.access.log;
error_log /var/log/example.com/nginx.error.log;
ssl_certificate /etc/nginx/ssl/example.com.pem;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
keepalive_timeout 5;
root /var/www/apps/example.com/dist;
charset utf-8;
location ~ ^/(scripts.*js|styles|images) {
gzip_static on;
expires 1y;
add_header Cache-Control public;
add_header ETag "";
break;
}
location /api1 {
rewrite ^/api1/(.*) /$1 break;
proxy_redirect off;
proxy_pass https://api1.example.com;
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 https;
proxy_set_header Authorization $http_authorization;
}
location /api2 {
rewrite ^/api2/(.*) /$1 break;
proxy_redirect off;
proxy_pass https://api2.example.com;
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 https;
proxy_set_header Authorization $http_authorization;
}
location / {
try_files $uri /index.html;
}
}
@dejan7
Copy link

dejan7 commented Apr 2, 2015

+1, i would like to know the same.

@r3wt
Copy link

r3wt commented Aug 13, 2015

hmm, hit a problem. now it doesn't work. I wanted to be able to handle both uri and query string in the angular app, and also have an api in the subdirectory.

doesn't work though:

location ~ ^/(api) {
    rewrite ^/api/(.*) /$request_uri break;
    index index.php;
    try_files $request_uri $request_uri/ /index.php?$query_string;
}

location / {
    include /etc/nginx/mime.types;
    try_files $uri /index.html?$query_string;
}

@farisridho
Copy link

plase explain me im newbie on this.

  1. are client and server must be on different domain like (client: example.com and server: srv.example.com)
  2. i just 1 ssl for 1 domain and not suport for subdomain. if qst no.1 true so i have got 2 ssl to make this work ?
  3. our system use nodejs on backend and angularjs on fronted and run under nginx. must i set ssl on client and server ( i have already set ssl on server side)

thanks for your answer

@ietuday
Copy link

ietuday commented Apr 21, 2017

I want to load balance an Multiple Mean App through NGINX. How can I do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment