Skip to content

Instantly share code, notes, and snippets.

@cjus
Last active July 12, 2023 14:59
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save cjus/b46a243ba610661a7efb to your computer and use it in GitHub Desktop.
Save cjus/b46a243ba610661a7efb to your computer and use it in GitHub Desktop.
AngularJS Nginx and html5Mode
server {
server_name yoursite.com;
root /usr/share/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
@MaestroJurko
Copy link

It doesn't work for me. It always redirects to 127.0.0.1:8080/

nginx:

    location / {
        autoindex on;           
        root   share/nginx/html;
    }

    location /adm {
        alias share/nginx/html/dev2;

        expires -1;
        add_header Pragma "no-cache";
        add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";

        try_files $uri$args $uri$args/ $uri/ /index.html =404;
    }

angularjs:

      $routeProvider
        .otherwise({
            redirectTo: '/adm/main2/'
        });

@rudeb0t
Copy link

rudeb0t commented Jan 9, 2015

I ran into the same problem that @mato75 encountered. I was using try_files with alias as well. I had to rebuild Nginx with debug enabled to figure out what was going on. Anyway...

    location /adm/ { # trailing slash here
        alias share/nginx/html/dev2/; # and here as well

        expires -1;
        add_header Pragma "no-cache";
        add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";

        try_files $uri$args $uri$args/ $uri/ /adm/index.html =404; # /adm/index.html and not /index.html
    }

Make sure the location has trailing slash as well as the aliased directory. And use /adm/index.html instead of just /index.html

@dimitardanailov
Copy link

Thank you.

@levivm
Copy link

levivm commented Nov 5, 2015

This saved my day, thx.

@scottsword
Copy link

Was banging my head on my desk trying to get this to work. If anyone is having path issues with static assets, be sure to declare your base tag above assets like css.

@mateussmohamed
Copy link

Hi guys !
Just some information to supplement. I had trouble making the configuration mentioned by our friend, had to do a restart of the settings for her always applied.
I used the following commands after the implementation of configuration in nginx.conf

- sudo nginx -t

  • This command is used to check for problems in nginx configuration, when I tried to reload nginx me he was returning a failure, needed to run this command to test the settings.
      Reference: https://www.digitalocean.com/community/questions/service-nginx-restart-fail
  • sudo service nginx reload
    to recharge the settings
  • sudo service nginx restart
    stop restart the service nginx

Tks.

@dj2bee
Copy link

dj2bee commented Apr 11, 2016

Awesome! This saved me a lot of time.

@aarmora
Copy link

aarmora commented Apr 22, 2016

This is great and also saved me a lot of time. Well done!

@Codenator81
Copy link

Solve my problem on Angular 2

@benallamar
Copy link

Thx @rudeb0t, you saved my day 👍

@jpduckwo
Copy link

jpduckwo commented Aug 3, 2016

I'm running nginx locally with docker and needed to adjust the settings to this:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
    }
...

@pulyaevskiy
Copy link

The example from @jpduckwo for Docker setup worked for me.

@rcshubhadeep
Copy link

Really helped me. Thanks 👍

@kentoj
Copy link

kentoj commented Oct 10, 2016

Thanks @jpduckwo for the try_files and @mato75 for the expires and cache configurations. My finished config looks like this:

server {
    listen       80;
    server_name  localhost;


    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        expires -1;
        add_header Pragma "no-cache";
        add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
        try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
    }


}

And my Dockerfile looks like this:

# Dockerfile
#

FROM nginx:1.11.3-alpine
MAINTAINER Kent Johnson <my email@gmail.com>

COPY dist/ /usr/share/nginx/html
COPY nginx/default.conf /etc/nginx/conf.d/default.conf

@rbo13
Copy link

rbo13 commented Nov 11, 2016

How can I configure my nginx that ignores #? I've added html5mode to my js file, but if I refresh the page when I'm in /home I get a 404 not found error from nginx and if I will add #/home in the URL everything will be okay. How do I fix this one?

@SamuelMarks
Copy link

@jpduckwo $args did the trick, thanks

@semos
Copy link

semos commented Dec 8, 2016

@whaangbuu, nginx does not see the hash part, browsers never sends it. If you look into your logs, you won't see it.

@anandchakru
Copy link

@Whaangbuu, thats not an nginx issue, angular2 issue - refer the docs - change yours to PathLocationStrategy from HashLocationStrategy

@leonetosoft
Copy link

A solução funcionou para mim também.

@ChristianUlbrich
Copy link

@kentoj Your configuration works like a charm. Thanks! I have built a docker image based on it -> https://hub.docker.com/r/zalari/nginx-html5/

@kevinhowbrook
Copy link

kevinhowbrook commented Mar 6, 2017

Thanks @jpduckwo! and @ChristianUlbrich for the image

@pajingko
Copy link

@jpduckwo @ChristianUlbrich, they are Savior.
Thanks so much.

@mscoobby
Copy link

@cjus Thank you!

@psk11
Copy link

psk11 commented Aug 12, 2017

What about two angular apps?
`

    listen       80;
    server_name  localhost;


    location /app1 {
        root /usr/share/nginx/app1;
        index index.html index.htm;
    }

    location /app2 {
        root /usr/share/nginx/app2;
        index index.html index.htm;
    }

`

What will be the nginx configs for two angular apps deployed on same server with html5mode? The above example is working fine for hashbang url's but not for the former one.

@SamuelMarks
Copy link

@psk11 This worked for me:

location /site0 {
    try_files $uri$args $uri$args/ /index.html;
    root   /var/www/static/site0/dist;
    index  index.html index.htm;
}

location /site1 {
    try_files $uri$args $uri$args/ /index.html;
    root   /var/www/static/site1/dist;
    index  index.html index.htm;
}

@sumitramteke
Copy link

Thanks @jpduckwo, your solution works for me. I guess in gist there's no way to thumbs up.

@cescgie
Copy link

cescgie commented Nov 28, 2017

@cjus this saves my time

@clov0
Copy link

clov0 commented Jan 17, 2018

Really Helped
Thanks

@mattisebastian
Copy link

You guys saved me a lot of time, thank you!

@royge
Copy link

royge commented Oct 25, 2018

Thanks @jpduckwo

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