【问题标题】:Nginx rewrite rule path to query stringNginx 重写规则路径到查询字符串
【发布时间】:2026-01-16 16:40:01
【问题描述】:

例子:

example.com/asd -> example.com/portal.php?id=asd

example.com -> example.com/portal.php

example.com/asd?document=new -> example.com/portal.php?id=asd&document=new

目前仅适用于: example.com -> example.com/portal.php

这是我目前所拥有的:

server {
    listen 80;
    listen [::]:80;

    root /var/www/html/public_html;

    server_name example.com;
    if ($http_x_forwarded_proto = 'http'){
        return 301 https://$host$request_uri;
    }

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /portal.php$is_args$args;
    }

    # LEGACY
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(portal)\.php(/|$) {
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param HTTPS on;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
       internal;
    }


    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

【问题讨论】:

    标签: regex nginx mod-rewrite url-rewriting


    【解决方案1】:

    你可以这样做:

    server {
        listen 80;
        listen [::]:80;
    
        root /var/www/html/public_html;
    
        server_name example.com;
        if ($http_x_forwarded_proto = 'http'){
            return 301 https://$host$request_uri;
        }
    
        # landing page
        location =/ {
            # try to serve file directly, fallback to index.php
            try_files $uri /portal.php$is_args$args;
        }
    
        # other pages
        location / {
           rewrite ^(.*)$ /portal.php?id=$1 last
        }
    
        # LEGACY
        # This rule should only be placed on your development environment
        # In production, don't include this and don't deploy app_dev.php or config.php
        location ~ ^/(portal)\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param HTTPS on;
            # When you are using symlinks to link the document root to the
            # current version of your application, you should pass the real
            # application path instead of the path to the symlink to PHP
            # FPM.
            # Otherwise, PHP's OPcache may not properly detect changes to
            # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
            # for more information).
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
           internal;
        }    
    
        # return 404 for all other php files not matching the front controller
        # this prevents access to other php files you don't want to be accessible.
        location ~ \.php$ {
            return 404;
        }
    
        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
    }
    

    【讨论】:

    • 加载资产时不工作例如:example.com/assets/styles/remodal/dist/remodal.css
    • 它会加载网站,然后转到:# other pages location / { rewrite ^(.*)$ /portal.php?id=$1 last }
    • 对不起,但这并不能告诉我您遇到了什么错误?
    【解决方案2】:

    这样就可以了:

    location / { 
        try_files $uri $uri/ @rules; 
    } 
    
    location @rules { 
    rewrite ^(.*)$ /portal.php?id=$1 last;
    }
    

    【讨论】: