Home > Backend Development > PHP Tutorial > nginx-several questions about location pattern

nginx-several questions about location pattern

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-29 08:50:55
Original
1293 people have browsed it

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

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

<code>location /doc {
    alias /home/user/doc;
}
</code>
Copy after login
Copy after login

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.

<code>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;
    }
}
</code>
Copy after login
Copy after login

Configure it like this on the main server:

<code>server {
    listen 80;
    ....
    location /doc {
        proxy_pass http://localhost:8000/;
    }
    }
</code>
Copy after login
Copy after login

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

<code>location /doc/ {
            proxy_pass http://localhost:8000/;
        }
</code>
Copy after login
Copy after login

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:

<code>location /doc {
    alias /home/user/doc;
}
</code>
Copy after login
Copy after login

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.

<code>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;
    }
}
</code>
Copy after login
Copy after login

Configure it like this on the main server:

<code>server {
    listen 80;
    ....
    location /doc {
        proxy_pass http://localhost:8000/;
    }
    }
</code>
Copy after login
Copy after login

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

<code>location /doc/ {
            proxy_pass http://localhost:8000/;
        }
</code>
Copy after login
Copy after login

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?

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
Error restarting nginx
From 1970-01-01 08:00:00
0
0
0
server - Nginx configuration webapp problem
From 1970-01-01 08:00:00
0
0
0
Nginx default.conf problem
From 1970-01-01 08:00:00
0
0
0
centos7 - NGINX exception occurs
From 1970-01-01 08:00:00
0
0
0
nginx load balancing
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template