Ps: You need to restart Nginx after modifying the configuration. Command: systemctl restart nginx. Nginx configuration file: /etc/nginx/nginx.conf
1. Syntax format
- Rewrite does what it does: Rewrite a URL to a specific URL.
- Syntax format:
rewrite
(Key words)<regex>
(Regular expression)<replacement>
(Alternative content). - According to the
Regular expression
Redirected toreplacement
.
2. Redirect all access from domain name A to domain name B
- Before the configuration
server
Cannot exist inlocation / { ... }
. - Configuration example:
location / {
rewrite ^/(.*) http://1000.xidian.edu.cn/$1;
}
Copy the code
- Before jump: a.com/index.php/R… .
- After the jump:
- through
location /
Matches all of the/
The initial request (that is, all requests). rewrite
Is the fixed keyword.regex
Partial matches the full domain name and subsequent paths.replacement
Part of theThe $1
Taken from theregex
Part of the( )
The content of.- The nginx will
a.com/
The next path is concatenatedhttp://1000.xidian.edu.cn/
After.
3. Redirect different accesses to domain NAME A to different domain names
① Application Scenarios
- Different urls in the same domain name need to be switched to different domain names.
(2) the sample a
location /xiaoyuan/ {
rewrite ^/xiaoyuan(.*) http://ehall.xidian.edu.cn$1;
}
Copy the code
- Before the jump:
http://a.com/xiaoyuan/jwapp/sys/cjcx
- After the jump:
- through
location /xiaoyuan/
Matches all of the/xiaoyuan/
Request at the beginning. rewrite
Is the fixed keyword.regex
Partial matches the full domain name and subsequent paths.replacement
Part of theThe $1
Taken from theregex
Part of the( )
The content of.- The nginx will
a.com/xiaoyuan
The next path is concatenatedhttp://ehall.xidian.edu.cn
After.
(3) the sample 2
location /houqin/ {
rewrite ^/houqin(.*) http://1000.xidian.edu.cn$1;
}
Copy the code
- Before the jump:
http://a.com/houqin/index.php/Request/...
- After the jump:
- through
location /houqin/
Matches all of the/houqin/
Request at the beginning. rewrite
Is the fixed keyword.regex
Partial matches the full domain name and subsequent paths.replacement
Part of theThe $1
Taken from theregex
Part of the( )
The content of.- The nginx will
a.com/houqin
The next path is concatenatedhttp://1000.xidian.edu.cn
After.
The appendix
- My personal blog is Messiwjy.top
- If you have any errors or doubts, please contact [email protected]