V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
mafeifan
V2EX  ›  问与答

求一个 nginx 地址重写规则

  •  
  •   mafeifan · 2018-07-19 16:07:29 +08:00 · 881 次点击
    这是一个创建于 2080 天前的主题,其中的信息可能已经有所发展或是发生改变。

    当匹配到地址 http://xxx/sites/app/avatar/0F0993200F910338F0EE638EFDFA1779 要求请求 http://xxx/sites/app/index.php?m=user&c=index&a=avatar&user_id=0F0993200F910338F0EE638EFDFA1779

    下面的写法好像不行

    	location / {
    	   rewrite ^sites/app/avatar/(.*)$  /sites/app/index.php?m=user&c=index&a=avatar&user_id=$1 last;
           try_files $uri $uri/ =404;
    	}
    
    5 条回复    2018-07-19 16:48:54 +08:00
    yangg
        1
    yangg  
       2018-07-19 16:16:05 +08:00
    rewrite ^/sites....$

    少了个 /
    mafeifan
        2
    mafeifan  
    OP
       2018-07-19 16:27:47 +08:00
    location / {
    rewrite ^sites/app/avatar/(.*)$ /sites/app/index.php?m=user&c=index&a=avatar&user_id=$1 last;
    try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    }

    还是报 404,求助啊
    mafeifan
        3
    mafeifan  
    OP
       2018-07-19 16:40:02 +08:00
    rewrite ^/sites/app/avatar/(.*)$ /sites/app/index.php?m=user&c=index&a=avatar&user_id=$1 last;
    wu67
        4
    wu67  
       2018-07-19 16:40:47 +08:00
    \/sites\/app\/avatar\/(.*)$

    这样?
    imdong
        5
    imdong  
       2018-07-19 16:48:54 +08:00
    不需要 location 字段也可以噻

    server {
    listen 80;
    root "/web";

    # 重写规则
    rewrite ^/sites/app/avatar/(.*)$ /sites/app/index.php?m=user&c=index&a=avatar&user_id=$1 last;

    location ~ \.php(.*)$ {
    fastcgi_pass 127.0.0.1:9000;
    ...
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2475 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:47 · PVG 23:47 · LAX 08:47 · JFK 11:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.