mafeifan
V2EX  ›  问与答

求一个 nginx 地址重写规则

  •  
  •   mafeifan · Jul 19, 2018 · 1326 views
    This topic created in 2870 days ago, the information mentioned may be changed or developed.

    当匹配到地址 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 replies    2018-07-19 16:48:54 +08:00
    yangg
        1
    yangg  
       Jul 19, 2018
    rewrite ^/sites....$

    少了个 /
    mafeifan
        2
    mafeifan  
    OP
       Jul 19, 2018
    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
       Jul 19, 2018
    rewrite ^/sites/app/avatar/(.*)$ /sites/app/index.php?m=user&c=index&a=avatar&user_id=$1 last;
    wu67
        4
    wu67  
       Jul 19, 2018
    \/sites\/app\/avatar\/(.*)$

    这样?
    imdong
        5
    imdong  
       Jul 19, 2018
    不需要 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;
    ...
    }
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   936 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 21:36 · PVG 05:36 · LAX 14:36 · JFK 17:36
    ♥ Do have faith in what you're doing.