Q
Q
q-sn2018-08-20 20:37:30
Nginx
q-sn, 2018-08-20 20:37:30

How to correctly register locations in Nginx?

There are 2 entities: landing and SPA. It is necessary that by going to the address http://127.0.0.1landing files are given, and by any other ( http://127.0.0.1/sing-inetc.) SPA files.
Stuck on the following:

server {
    listen 80;

    server_name localhost;
    index index.html;

    location = / {
        root C:\Workspace\landing;
    }

    location ~ ".+" {
        alias C:\Workspace\frontend\dist\app-frontend;
        try_files $uri $uri/ /index.html;
    }
}

The problem is that when you go to the root ( http://127.0.0.1), SPA files are given, i.e. the first location fails.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2018-08-21
@q-sn

nginx.org/ru/docs/http/ngx_http_index_module.html#index

Keep in mind that when using an index file, an internal redirection is made and the request may be processed in another location. For example, in this configuration:
location = / {
    index index.html;
}

location / {
    ...
}

the request for “/” will actually be processed in the second location as “/index.html”.

Therefore, location = /try_files is needed instead of index.
In addition to /index.html, the landing page also has scripts, pictures, styles
C:\Workspace\landing:
-css/
-images/
-js/
-index.html
server {
    listen 80;

    server_name localhost;
    index index.html;

    root C:/Workspace/landing;
    
    location = / {
        try_files /index.html =404;
    }
    
    location /images/ { }
    location /css/    { }
    location /js/     { }
    
    
    location / {
        root C:/Workspace/frontend/dist/app-frontend;
        try_files $uri $uri/ /index.html;
    }
}

K
ky0, 2018-08-20
@ky0

location / {
...sign-in
}

location = / {
...landing
}

https://nginx.org/ru/docs/http/request_processing.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question