Home  >  Article  >  Backend Development  >  nginx-several questions about location pattern

nginx-several questions about location pattern

WBOY
WBOYOriginal
2016-08-29 08:50:551129browse

I checked a lot of information but couldn’t figure out a few questions.

The first question, if my location configuration is like this:

location /doc {
    alias /home/user/doc;
}

Then when I visit http://localhost/doc/a.html, nginx actually reads /home/usr/doc/a.html. If I visit http://localhost /docs/a.html or even http://localhost/docsioajsfopajowejfasd So which file will nginx actually try to read?

Second question, if I configure doc as a server and then reverse proxy.

server {
    listen 8000;
    server_name doc;
    root /home/user/doc;
    
    index index.html index.htm index.nginx-debian.html;

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

Configure it like this on the main server:

server {
    listen 80;
    ....
    location /doc {
        proxy_pass http://localhost:8000/;
    }
    }

When accessing http://localhost/doc/ when configured in this way, if the index file references a static file, the static file will become 404, and the browser will try to obtain http://localhost/js/xxx.js instead of http://localhost/doc/js/xxx.js, if you add / after pattern it becomes

location /doc/ {
            proxy_pass http://localhost:8000/;
        }

There is no problem, but if it is the location configuration in the first question, the browser will correctly find http://localhost/doc/js/xxx.js. This makes me very confused. What is the impact of adding / at the end? Why do alias and proxy_pass have different results?

Reply content:

I checked a lot of information but couldn’t figure out a few questions.

The first question, if my location configuration is like this:

location /doc {
    alias /home/user/doc;
}

Then when I visit http://localhost/doc/a.html, nginx actually reads /home/usr/doc/a.html. If I visit http://localhost /docs/a.html or even http://localhost/docsioajsfopajowejfasd So which file will nginx actually try to read?

Second question, if I configure doc as a server and then reverse proxy.

server {
    listen 8000;
    server_name doc;
    root /home/user/doc;
    
    index index.html index.htm index.nginx-debian.html;

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

Configure it like this on the main server:

server {
    listen 80;
    ....
    location /doc {
        proxy_pass http://localhost:8000/;
    }
    }

When accessing http://localhost/doc/ when configured in this way, if the index file references a static file, the static file will become 404, and the browser will try to obtain http://localhost/js/xxx.js instead of http://localhost/doc/js/xxx.js, if you add / after pattern it becomes

location /doc/ {
            proxy_pass http://localhost:8000/;
        }

There is no problem, but if it is the location configuration in the first question, the browser will correctly find http://localhost/doc/js/xxx.js. This makes me very confused. What is the impact of adding / at the end? Why do alias and proxy_pass have different results?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn