Most of the questions in the FAQ come from http://www.stackoverflow.com and http://www.serverfault.com, some of which may not have been verified by me.
Nginx How to replace errors
Q: Can I return with a 503 error (overloaded or temporarily unavailable) instead of 502 as an answer?
A: Configure the fastcgi_intercept_errors directive and set it to on, and then use the error_page directive:
Location/{Fastcgi_pass 127.0.0.1:9001; fastcgi_intercept_errors on; Error_page 502 =503/error_page.html;
Nginx how to rewrite 404 to other pages
Q: Can I rewrite the requested nonexistent file to index.php?
A: Use the try_files directive:
Try_files $uri $uri//index.php
$uri and $uri/will determine whether the requested URI is an existing file or directory, and if not, it will be rewritten to the last argument, index.php
Nginx How to save the requested host header for the agent's request
Q: When using Nginx's Proxy_pass proxy for a backend server (application), if the back-end server has multiple virtual hosts, the proxy request does not produce the correct answer (no host request header in the Nginx-initiated request), how to solve the problem?
A: Set the host header using the Proxy_set_header directive:
Location/{proxy_pass http://my_app_upstream proxy_set_header Host $host;
How to configure the Nginx rewrite, will not cause the browser URL address redirection?
The P parameter in the Q:apache rewrite rule is represented as a force proxy, even if this parameter can be used to say one domain name rewrite to another domain name without causing the URL of the browser address bar to change, nginx how to achieve this function?
A: Use rewrite with Proxy_pass:
Location ~ ^/frompath/{rewrite ^/frompath/(. *) $/topath/$1 break; Proxy_pass http://www.domain.com;}
If the request is http://www.test.com/frompath/page.php, it will be rewritten to http://www.domain.com/topath/page.php without causing a change in the URL in the browser's address bar
How do I override a URI with a parameter?
Q: How to use Nginx to rewrite/test.php?id=1&action=delete to/action.php?id=1&action=delete
A: The parameter is saved in the variable $query_string, and the default parameter follows the overridden URL, which adds a question mark to the new rule:
Set $query $query _string;rewrite/test.php/action.php $query?
If you require that the overridden parameter be different from the parameter before the rewrite, you can make a regular match for the $query_string variable and set the variable with the set so that you can use them in the rewrite rule.