In daily operation and maintenance, we often encounter situations where we need to jump from one link to another. Such requirements are divided into two situations, one is that the browser URL changes, and the other is that the browser URL does not change.
1. Url change, use nginx rewirte module
Rewrite is a key instruction to achieve URL rewriting. According to the regex (regular expression) part of the content, it is redirected to replacement, and the end is flag.
The flag mark is divided into the following four types:
last #After this rule is matched, continue to match the new location URI rule downward
break #This rule is terminated when the match is completed, and no longer matches any subsequent rules
redirect #Return to 302 temporary redirection, the browser address will display the URL address after the redirection
permanent #Return to 301 permanent redirection, the browser address bar will display the redirected URL address
301 is often used to realize the URL address redirection function.
2. The URL and address remain unchanged, and both location and rewrite modules can be used
The proxy_pass module of lcaotion; or the last and break of rewrite can be used to launch this function.
(1), configuration
Jump the request path https://api.gwhome.com/data/app to the https://images.com/data/app/gw.html page.
server {
listen 443;
server_name gwhome
access_log /data/nginx/logs/gwhome-access.log main;
error_log /data/nginx/logs/gwhome-error.log;
ssl on;
ssl_certificate /data/nginx/ssl/gwhome.crt;
ssl_certificate_key /data/nginx/ssl/gwhome.key;
ssl_session_timeout 5m;
location = /data/app{
rewrite /data/app /data/app/gw.html break;
proxy_pass https://images.com;
}
}
(2), configuration
Jump the request to access 192.168.210.85:8190/gwgou/order/commdany to 192.164.60.89:8089/order/commdany
server {
listen 8190;
server_name 192.168.210.85;
index index.html index.php index.htm;
location ~ * ^ / gwgou / order / commdany {
proxy_next_upstream error timeout http_503 http_504 http_502;
proxy_connect_timeout 500s;
proxy_read_timeout 500s;
proxy_send_timeout 500s;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite ^(.*)$ /order/commdany break;
proxy_pass http://192.164.60.89:8089;
}
}
Recommended reading