nginx rewrite自定义url求解

新手上路,请多包涵

1、实现如下url重写
用户请求api.com/user.login?username=123&password=123时,
自动重写到api.com/user.php?act=login&username=123&password=123

2、nginx服务器配置如下,版本1.16.0
server

{
    listen 80;
    server_name api.com;
    index index.html index.htm index.php default.html default.htm default.php;
    root  /www/api.com/api;

    location / {
        #rewrite ^/(.*)\.(.*)\?.*$ /$1\.php?act=$2&$query_string break;
        #rewrite ^/(.*)\.(.*)\?.*$ /$1\.php?act=$2&$query_string last;
        #rewrite (.*)\.(.*)$ $1\.php?act=$2&$query_string break;
        #rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2&$query_string break;
        #rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2&$query_string last;
        #rewrite ^/(.*)\.(.*)$ /$1\.php?act=$2 last;
    }
    
    location ~ [^/]\.php(/|$)
    {
        try_files $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.
    {
        deny all;
    }
}

2.1、apache下如下写法是可以的

<Directory /www/api.com/api/>

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all

    Options All -Indexes
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f

    RewriteBase /
        RewriteRule (.*)\.(.*)$ $1\.php?act=$2&%{QUERY_STRING} [L]    
        ErrorDocument 404 /404.html
</Directory>

3、一直报404错误或者直接提示下载user.login文件

阅读 2.4k
1 个回答
location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    location / {   rewrite  ^(.*)\.(.*)$  /$1.php?act=$2&$query_string  last;   break;    }

试着写了一下……本地nginx在1.14.0上这么干可以正常访问……
不过感觉和题主的区别也不大,可能主要是少个last的问题

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题