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

**URL:** <https://www.ruby-forum.com/t/how-to-send-all-these-requests-to-the-same-file-when-i-have-an-angular-state-based-router/243228>\
**Category:** NGINX\
**Created:** [September 2, 2015, 7:38pm UTC](https://www.ruby-forum.com/t/how-to-send-all-these-requests-to-the-same-file-when-i-have-an-angular-state-based-router/243228 "2015-09-02T19:38:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [September 2, 2015, 7:38pm UTC](https://www.ruby-forum.com/t/how-to-send-all-these-requests-to-the-same-file-when-i-have-an-angular-state-based-router/243228/1 "2015-09-02T19:38:13Z")

</div>

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:  
  
RewriteEngine On  
# Required to allow direct-linking of pages so they can be  
processed by  
Angular  
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:

> **[How to send all these requests to the same file when I have an Angular state...](https://forum.nginx.org/read.php?2,261396,261396#msg-261396)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![maplesyrupandrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@maplesyrupandrew](https://www.ruby-forum.com/u/maplesyrupandrew)\
**Post date:** [September 2, 2015, 9:05pm UTC](https://www.ruby-forum.com/t/how-to-send-all-these-requests-to-the-same-file-when-i-have-an-angular-state-based-router/243228/2 "2015-09-02T21:05:16Z")

</div>

On Wed, Sep 02, 2015 at 01:37:49PM -0400, maplesyrupandrew wrote:

Hi there,

> 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.

From those words, and the Subject: line above, I’m not actually sure  
what  
it is that you want to do. “all requests to the same file” is one thing;  
but does not seem to be what the rest of your mail implies.

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

I think that means “send the file, else send the directory index, else  
send the fallback /index.html url”.

Is that correct?

Because if so, that’s what try\_files is for.

> 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?

If you have “location /{}” and “location /auth{}”, then the first one  
will match all requests that do not start “/auth”.

Your third location seems odd. (It’s a prefix location, since it does  
not start with ~.)

> ```
> index index.html index.html;
> 
> ```

That line probably doesn’t do much useful.

> ```
> server_name _;
> 
> ```

That line probably doesn’t do much useful.

> ```
> location / {
> # First attempt to serve request as file, then
> # as directory, then fall back to displaying a 404.
> try_files $uri $uri/ =404;
> 
> ```

But you want “try as file, then as directory, then fall back to  
/index.html”, no?

try\_files $uri $uri/ /index.html;

> ```
> }
> 
> location /auth{
> proxy_pass http://auth;
> }
> 
> location /^(?!auth$).* {
> try_files $uri /var/www/..../dist/index.html;
> }
> 
> ```

Remove that location{} altogether. It probably won’t do any harm, as it  
is unlikely to match any request. But it is confusing.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![maplesyrupandrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@maplesyrupandrew](https://www.ruby-forum.com/u/maplesyrupandrew)\
**Post date:** [September 8, 2015, 2:17am UTC](https://www.ruby-forum.com/t/how-to-send-all-these-requests-to-the-same-file-when-i-have-an-angular-state-based-router/243228/3 "2015-09-08T02:17:22Z")

</div>

Thank you Francis - the “try” line was key 🙂

Posted at Nginx Forum:

> **[Re: How to send all these requests to the same file when I have an Angular...](https://forum.nginx.org/read.php?2,261396,261495#msg-261495)**
>
> 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...
