dqdes60666 2018-05-20 11:48
浏览 435
已采纳

Nginx + PHP-FPM重定向到静态PHP文件

Some details about my setup first:

  • I am serving a static webapp (HTML + JS) from default Nginx webroot
  • I have a PHP-FPM server running on localhost:9000
  • The destination file should be /api/webroot/index.php for FPM (always, no need to try_files etc.)
  • I need to forward all /api and /api-debug calls to arrive at localhost:9000, and the /app/webroot/index.php should handle all these requests.

I have the following working Nginx configuration:

upstream fastcgi_backend {
    server localhost:9000;
    keepalive 30;
}

server {
    listen   80;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

        location ~ ^/(api|api-debug)/ {
            root       /app/webroot;
            index      index.php;
            try_files  $uri /api/index.php$is_args$args;

            location ~ \.php$ {
                fastcgi_pass   fastcgi_backend;

                fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*)$;
                fastcgi_param  SCRIPT_FILENAME /app/webroot/$fastcgi_script_name;

                include        fastcgi_params;
            }
        }
    }
}

I just want to make it more simple and efficient, because as I see it now it's a mess. I tried to adjust for example

try_files $uri /api/index.php$is_args$args;

to

try_files $uri /api/webroot/index.php$is_args$args;

and it failed... The only reason that it works is that /api/index.php includes /api/webroot/index.php, but I see it's inefficient.

I found debugging nginx config hard, because it's not easy to test.

Thank you very much for your help in advance!

  • 写回答

1条回答 默认 最新

  • douwen7516 2018-05-20 16:00
    关注

    The simplest solution would be to hardwire SCRIPT_FILENAME with a value of /app/webroot/index.php and remove one of your location blocks altogether.

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location ~ ^/(api|api-debug)/ {
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME /app/webroot/index.php;
        fastcgi_pass   fastcgi_backend;
    }
    

    Alternatively, to keep the flexibility of specifying a URI with a .php extension, you could simplify the configuration with:

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    
        rewrite ^/(api|api-debug)/ /index.php last;
    }
    
    location ~ \.php$ {
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME /app/webroot$uri;
        fastcgi_pass   fastcgi_backend;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 汇川EASY521plc电子凸轮
  • ¥15 C++ 如何判断设置快捷键来实现隐藏/显示窗口
  • ¥15 关于#材料工程#的问题:有没有具有电子阻挡层和空穴阻挡层的电池仿真silvaco代码例子或者其他器件具有阻挡层例子的silvaco代码(最好还有相关文献)
  • ¥60 基于MATLAB的TAOD算法
  • ¥15 Groimp使用疑问
  • ¥15 MDK–ARM里一直找不到调试器
  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图