TheRiver | blog

You have reached the world's edge, none but devils play past here

0%

proxy_pass return rewrite try_file

dns

www.riverferry.site 34.92.9.255

nginx.org 3.125.197.172

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 0.0.0.0:8888;

server_name www.riverferry.site;

location / {
proxy_pass http://nginx.org;
}
}

server {
listen 0.0.0.0:9999;

server_name www.riverferry.site;

location / {
return 301 http://nginx.org;
}
}

proxy_pass



return



rewrite和return的区别

rewrite regex URL [flag];

详情见Creating NGINX Rewrite Rules

  • the first argument, regex, means that NGINX Plus and NGINX rewrite the URL only if it matches the specified regular expression (in addition to matching the server or location directive). The additional test means NGINX must do more processing.
  • A second difference is that the rewrite directive can return only code 301 or 302. To return other codes, you need to include a return directive after the rewrite directive (see the example below).
  • And finally the rewrite directive does not necessarily halt NGINX’s processing of the request as return does, and it doesn’t necessarily send a redirect to the client. Unless you explicitly indicate (with flags or the syntax of the URL) that you want NGINX to halt processing or send a redirect, it runs through the entire configuration looking for directives that are defined in the Rewrite module (break, if, return, rewrite, and set), and processes them in order. If a rewritten URL matches a subsequent directive from the Rewrite module, NGINX performs the indicated action on the rewritten URL (often rewriting it again).

try_files

try_files file … uri;

In the following example, NGINX serves a default GIF file if the file requested by the client doesn’t exist. When the client requests (for example) http://www.domain.com/images/image1.gif, NGINX first looks for image1.gif in the local directory specified by the root or alias directive that applies to the location (not shown in the snippet). If image1.gif doesn’t exist, NGINX looks for image1.gif/, and if that doesn’t exist, it redirects to /images/default.gif. That value exactly matches the second location directive, so processing stops and NGINX serves that file and marks it to be cached for 30 seconds.

1
2
3
4
5
6
7
location /images/ {
try_files $uri $uri/ /images/default.gif;
}

location = /images/default.gif {
expires 30s;
}
----------- ending -----------