1.rewrite配置
server { listen 80 default_server; server_name jeson.t.imooc.io; access_log /var/log/nginx/log/host.access.log main; root /opt/app/code; location ~ ^/break { rewrite ^/break /test/ break; # break 重定向到/test 不会重新发送请求 } location ~ ^/last { rewrite ^/last /test/ last; # last 重定向到/test 会重新发送/test请求 } location ~ ^/imooc { rewrite ^/imooc http://www.imooc.com/ permanent; # 永久重定向 下次访问不会请求 nginx 直接访问镜像地址 #rewrite ^/imooc http://www.imooc.com/ redirect; # 临时重定向 每次访问都会请求一次 nginx } location / { rewrite ^/course-(\d+)-(\d+)-(\d+)\.html$ /course/$1/$2/course_$3.html break; if ($http_user_agent ~* Chrome) { # 判断浏览器是否是chrome 浏览器 rewrite ^/nginx http://coding.imooc.com/class/121.html redirect; } if (!-f $request_filename) { # 判断请求的文件的路径是发存在 ! 非 取反的意思 -f 判断路径用的 rewrite ^/(.*)$ http://www.baidu.com/$1 redirect; } index index.html index.htm; } location /test/ { default_type application/json; return 200 '{"status":"success"}'; } }
2.rewrite优先级
server > location