preface

I learned about nginx a few days ago, and then prepared to practice myself, but found that after configuring myself, I could not access the home page, but there was no problem accessing other pages, I did not know why for a long time, and finally I asked someone else to help solve the problem, first posted the nginx configuration

The main content

This is a misconfiguration from the start

server { listen 80; server_name example.com; location = / { root /home/projects/temporary; index index.html; } location /test { alias /home/projects/temporary/test; } location /test2 { alias /home/projects/temporary; } location ~* .*\.(png|html|js|css|jpg)$ { root /home/projects/temporary/static; }}Copy the code

The main reasons why index.html cannot be accessed are as follows: This problem is caused by the fact that I thought the exact configuration was /, and then/the following specified index.html can also be accessed, but the exact match only matches /, and the actual request /index.html cannot be accessed. Method 1. If you want to use an exact match, you need to know how it works. For example, if you want to use an exact match, you need to know how it works. If you want to use an exact match, you need to know how it works. At this point we can add a pre-match rule

location ^~ /index.html  {
        root /home/projects/temporary;
}
Copy the code

Since his priority is higher than regex’s, then index. HTML is underneath /home/projects/temporary and the page will display correctly

Method 2. Remove the parameters of the exact match, and remove the HTML in the re match, because the standard URL (i.e. the match with no parameters) has the lowest priority. If the HTML in the re is not removed, this rule will be matched first. Then as the index. HTML is not in the/home/projects/temporary/static folders, won’t be so visit mydomain.com/index.html access error

Write in the last

Finally, I attached my configuration, in fact, the key is to understand the principle, so that the problem can be solved quickly and efficiently, with ease

server { listen 80; server_name example.com; location = / { root /home/projects/temporary; index index.html; } location ~* .*\.(png|js|css|jpg|html) { root /home/projects/temporary/static; } location ^~ /index.html { root /home/projects/temporary; }}Copy the code