A newbie using nginx, I have a few questions for the experts.
为情所困
为情所困 2017-05-16 17:23:54
0
2
537

I saw before that MOOC uses nginx to deploy static resources, so I wanted to try using ngjinx to deploy my resources. Now we need the following questions:

  1. For example, this link on MOOC.com: http://www.imooc.com/course/list, how does it match /course/list after the url, I can only use list.html, I want every connection not to have the suffix .html. Is this to rewrite the URL, or use location configuration

2.

The above is a location configuration, used for reverse proxy. It should not be able to match something like /test/test.html, but it did not match, so it was not forwarded to the proxy server, but Display static pages normally.

But it can match this: /products/getproducts/2, and it is indeed forwarded to: localhost:8086/products/getproducts/2, and the correct data is obtained.

Let’s ask these two questions first. I don’t know much about servers. I might be a bit of an idiot. I hope you can answer them

为情所困
为情所困

reply all(2)
大家讲道理

First question:
Nginx uses the try_files directive to easily import request data into the framework’s front-end controller such as index.php:

location {
    try_files $uri $uri/ /index.php?$args;
}

The front-end controller index.php can use $_SERVER['REQUEST_URI'] to obtain the string of URL parameters /course/list. If there are parameters later, such as /course/list?page=2&tag=mysql, you can use $_SERVER ['QUERY_STRING'] Get the query string page=2&tag=mysql, then urldecode it yourself and use explode to split the string with & to get a parameter array similar to $_GET. You can also use $_GET directly to get the parameter array.

In short, it is the program entrance index.php and the program inside it that decide whether to load the HTML static file and return it, or connect to the database for processing.
If you want Nginx to determine whether the HTML cache is hit, you can configure it like this:

location / {
    try_files $uri $uri/ /html$uri.html /index.php?$args;
}

For example, when accessing /post/1024, Nginx will try to access the following files in the order given by try_files:
/post/1024 ($uri represents file)
/post/1024/ ($uri/ represents the directory)
/html/post/1024.html (/html$uri.html represents your HTML static cache)
If neither exists, the URI information along with the parameters will be handed over to index.php for processing (the last /index.php?$args expresses this meaning).

習慣沉默

Question 1, You can use the rewrite method, for example:

rewrite "^/(course/.*)/?"   .html last;

It should be noted that only all requests starting with course will be rewritten
xxx.com/course/list -> xxx.com/course/list.html
xxx.com/course/list/sub -> xxx.com/course/list/sub.html

Question 2
I feel like there should be other location configurations. If it’s convenient, post the entire nginx configuration

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!