How to send all these requests to the same file when I have an Angular state based router?

maplesyrupandrew nginx-forum at
Wed Sep 2 17:37:49 UTC 2015

I'm using the Angular ui-router which uses states to control the routes.

Meaning that all request should serve the same index.html file, and the
JavaScript worries about loading in appropriate content.

The .htaccess rules that control the same thing are below:
<ifModule mod_rewrite.c>
RewriteEngine On 
     # Required to allow direct-linking of pages so they can be processed by
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !index RewriteRule (.*) index.html [L]

I'm trying to achieve the same effect, but I'm serving content through
nginx. I tried to achieve this by adding the third location block in the
nginx config below, however, this didn't seem to do the trick (404s). It
tries to catch all routes that are not /auth.

What am I missing here?

server {
    listen 80 default_server;

    root /var/www/..../dist;
    index index.html index.html;

    # Make site accessible from http://localhost/
    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules

    location /auth{
        proxy_pass http://auth;

    location /^(?!auth$).* {
        try_files $uri  /var/www/..../dist/index.html;

Posted at Nginx Forum:,261396,261396#msg-261396

More information about the nginx mailing list