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 theRegular expressionRedirected toreplacement.

2. Redirect all access from domain name A to domain name B

  • Before the configurationserverCannot 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:

  • throughlocation /Matches all of the/The initial request (that is, all requests).
  • rewriteIs the fixed keyword.
  • regexPartial matches the full domain name and subsequent paths.
  • replacementPart of theThe $1Taken from theregexPart of the( )The content of.
  • The nginx willa.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:

  • throughlocation /xiaoyuan/Matches all of the/xiaoyuan/Request at the beginning.
  • rewriteIs the fixed keyword.
  • regexPartial matches the full domain name and subsequent paths.
  • replacementPart of theThe $1Taken from theregexPart of the( )The content of.
  • The nginx willa.com/xiaoyuanThe next path is concatenatedhttp://ehall.xidian.edu.cnAfter.

(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:

  • throughlocation /houqin/Matches all of the/houqin/Request at the beginning.
  • rewriteIs the fixed keyword.
  • regexPartial matches the full domain name and subsequent paths.
  • replacementPart of theThe $1Taken from theregexPart of the( )The content of.
  • The nginx willa.com/houqinThe next path is concatenatedhttp://1000.xidian.edu.cnAfter.

The appendix

  • My personal blog is Messiwjy.top
  • If you have any errors or doubts, please contact [email protected]