M
M
Michael2017-03-18 21:40:19
Nginx
Michael, 2017-03-18 21:40:19

Why is static not given?

Hello. Tried to set up nginx on localhost and ran into a problem. The static is returned, all requests are proxied, but when visiting localhost:3000 index.html is not displayed (instead I see 404 page not found).
When visiting localhost:3000/index.html everything is fine. Here is nginx.conf

server {
      # IP, который мы будем слушать
      listen 127.0.0.1:3000;

      location / {
    # IP и порт, на которых висит node.js
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    root /home/www/files;
    index index.html;
      }

      location ~ \.(html|css|js)$ {
    access_log off;
    expires 30d;
    root /home/www/files/;
      }
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry MiksIr, 2017-03-19
@mak_ufo

The config is normal, everything works exactly as you wrote. The request to/goes to 127.0.0.1:8080. The request for /index.html goes to another location and is returned statically. Since you did not describe how the system should behave, which requests should be static, which - dynamics, then there is no possibility to offer another config.
PS;
Option 1. Describe / as a separate exact match location

root /home/www/files;

location = / {
   index index.html;
}

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

location ~ \.(html|css|js)$ {
    access_log off;
    expires 30d;
}

The request / gets to "= /", the autoindex module will work there, check for the presence of /index.html on the disk, and if it finds it, it will make an internal redirect to /index.html, which will go to "location ~ \.(html|css|js )$".
Option 2. We simply check for the presence of files on the disk, and transfer them to the node only if there are none.
root /home/www/files;

location / {
    try_files $uri $uri/ @nodejs;
    index index.html;
}

location @nodejs {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

location ~ \.(html|css|js)$ {
    access_log off;
    expires 30d;
}

In this case, the request goes to "location /", where try_files checks for the presence of a file on disk or the presence of such a directory. The presence of the directory works (we have the directory /home/www/files/ on the disk), the request remains in the context of location /, the autoindex module will work, it will check for the presence of /index.html on the disk, and if it finds it, it will make an internal redirect to /index. html. If try_files did not find anything (for example, for the request /bla/bla/), then try_files makes an internal redirect to its last argument - the "named location", in which the work with the node is already taking place.
In principle, the second option is even preferable somewhere, but if you need the behavior "we transfer to the node on request /dir/file.txt even if there is /home/www/files/dir/file.txt on the disk" - then the first option.
Both options will give a 404 if the index.html file is removed from disk. If you need behavior that is index.html - we give it, no - we go to the node, then we change the config a bit.
Option 3
root /home/www/files;

location / {
    try_files $uri /index.html @nodejs;
}

location @nodejs {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

location ~ \.(html|css|js)$ {
    access_log off;
    expires 30d;
}

Only try_files has changed, now it does not check for the presence of a directory, but checks for the presence of two files, the requested one (in the case of a / request, there is no such file), and /home/www/files/index.html, regardless of the request URL.

D
Dimonchik, 2017-03-18
@dimonchik2013

it checks in the following order:
it finds content on index and calms down
or erase or make the node return what is there in index.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question