Nginx is a high-performance web server and reverse proxy server that is widely used for website deployment and traffic management. URL rewriting is one of the important functions in web development, which can be used to help optimize the URL structure to achieve SEO friendliness, and can also realize a variety of complex request routing logic. Using nginx rewrite directive can also easily implement URL rewriting rules.
The URL rewriting rules of the US CN2 cloud server Nginx have three main instructions: rewrite, return and try_files. The rewrite command is used to match and replace urls based on regular expressions, the return command can directly return new urls, and try_files is mainly used to search and redirect static files.
In Nginx configuration files, rewrite directives are usually placed in server, location, or if statement blocks. The basic syntax is:
rewrite regex replacement [flag];
regex is the URL to be matched by the regular expression, replacement is the new URL, and flag is used to control rewriting behavior. Common flags are:
last: Stops the current location processing and continues to match the new location.
break: Stops the current location processing and stops matching.
redirect: Return 302 Temporary redirect.
permanent: Returns 301 permanent redirection.
For example, if you want to rewrite all urls starting with /old/ to /new/, you can use the following configuration:
server {
listen 80;
server_name example.com;
location / {
rewrite ^/old/(.*)$ /new/$1 permanent;
}
}
The configuration above, said when visiting http://example.com/old/something, the browser will be a 301 redirect to http://example.com/new/something.
In addition, more complex URL rewriting can be achieved in combination with regular expressions. For example, to rewrite a URL like /blog/2025/03/25/article-name to /post/article-name, you can use:
rewrite ^/blog/\d{4}/\d{2}/\d{2}/(.*)$ /post/$1 last;
The return instruction is more efficient if you simply redirect one path to another without using a regular expression. For example:
location /old-page {
return 301 /new-page;
}
The try_files directive applies to static files or pseudo-static rules, such as URL rewriting rules that WordPress or other CMS systems may require:
location / {
try_files $uri $uri/ /index.php? $query_string;
}
This approach ensures that if the requested resource exists, it is returned directly, otherwise the request is forwarded to index.php for processing.
In practical applications, the URL rewriting rules of the US CN2 cloud server Nginx can be used with multiple location blocks and if conditions to achieve more flexible URL processing. For example:
server {
listen 80;
server_name example.com;
location / {
if ($request_uri ~* "old-page") {
rewrite ^(.*)$ /new-page permanent;
}
}
}
This rule redirects to "new-page" when it detects that the requested URI contains "old-page".
It is worth noting that when writing the URL rewriting rules of the US CN2 cloud server nginx, unnecessary rewrite rules should be minimized to improve the processing efficiency of Nginx. For simple redirection, the return command is recommended. For static resource matching, try_files is recommended. Use the rewrite directive only if complex URL rewriting is required.
In addition, URL rewriting rules can be optimized through the ngx_http_map_module module. For example, if you need to map multiple different paths to a new path, you can use the map directive:
map $request_uri $new_uri {
/old-page1 /new-page1;
/old-page2 /new-page2;
/old-page3 /new-page3;
}
server {
listen 80;
server_name example.com;
location / {
if ($new_uri) {
return 301 $new_uri;
}
}
}
This method can reduce the use of rewrite instruction and improve the performance of Nginx.
The above can obtain a variety of URL rewriting methods of nginx. Through reasonable application of rewrite, return and try_files directives, you can efficiently manage URL structure, optimize website performance and SEO friendliness. When implementing writing rules, it is best to choose the right method for your specific needs. Mainly pay attention to performance optimization, to ensure the stability of the US CN2 cloud server nginx operation. In addition to the above ways, there are many ways to optimize the performance and stability of the US CN2 cloud server, such as optimizing its network, storage, hardware configuration, etc., can achieve certain results.