Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
@AnirbanKundu

This comment has been minimized.

Copy link

commented Mar 31, 2015

Can you upload a github project on how to setup an angular application using NGINX server ? I am looking for the same but unable to find one. I want to call different routes from the angular app to the NGINX server. The call after it reaches the NGINX server, I want to redirect the call to another REST end point. Any help on this will be truly helpful .

Thanks,
Anirban

@dejan7

This comment has been minimized.

Copy link

commented Apr 2, 2015

+1, i would like to know the same.

@r3wt

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Aug 11, 2016

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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.