Skip to content

伪静态 URL

http://192.168.44.101/1.html => http://192.168.44.101/index.html?page=1

nginx
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  www.rainx.top;

        location / {
            # 当访问 1.html 时实际访问的是 /index.html?page=1
            # rewrite ^/1.html$ /index.html?page=1 break;

            # 当访问 n.html 时实际访问的是 /index.html?page=n
            rewrite ^/([0-9]+).html$ /index.html?page=$1 break;
            proxy_pass http://192.168.44.102;
        }

        location ~*/(js|css|img) {
            root html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

rewrite <regex> <replacement> [flag]; 关键字 正则 替代内容 flag标记

关键字:其中关键字error_log不能改变 正则:perl兼容正则表达式语句进行规则匹配 替代内容:将正则匹配的内容替换成replacement flag标记:rewrite支持的flag标记

rewrite参数的标签段位置: server,location,if

flag标记说明:

  • last

    本条规则匹配完成后,继续向下匹配新的location URI规则

  • break

    本条规则匹配完成即终止,不再匹配后面的任何规则

  • redirect

    返回302临时重定向,浏览器地址会显示跳转后的URL地址

  • permanent

    返回301永久重定向,浏览器地址栏会显示跳转后的URL地址