【问题标题】:Executing php scripts under a nginx configuration在 nginx 配置下执行 php 脚本
【发布时间】:2014-12-03 09:55:00
【问题描述】:

我的配置中有以下 nginx 服务器块

server {
    listen       80;
    server_name mydomain.com;
    location /deploy {
        alias /home/somedir/deploy/;
        }

    # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;

    }
  }

我想在 url mydomain.com/deploy/erp/index.php 或 mydomain.com/deploy/erp 处执行一个 php 文件

上面的东西是下载php文件而不是执行它。我搜索并找到了要求您定义 www 目录或其他内容的解决方案。我只需要在特定 url 的特定目录中执行文件。我有哪些选择?

【问题讨论】:

标签: php nginx


【解决方案1】:

你还没有告诉 nginx 如何处理 .php 文件。假设你使用的是 php-fpm,你需要这样的东西:

server {
  listen       80;
  server_name mydomain.com;

  root /home/somedir;

  location / {
    try_files $uri $uri/ =404;
  }

  location /deploy {
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
  }
}

【讨论】:

  • 它可以工作,但有一点改变。您的代码实际上可以在 ubuntu 版本 12.xx 及更低版本上运行。在这些之上,您需要传递 unix:/var/run/php5-fpm.sock 而不是 127.0.0.1:9000
  • 太棒了!这取决于您的 fpm 设置(例如 /etc/php/fpm/php-fpm.conf) - 您可以选择 TCP 或 unix 套接字(请参阅:github.com/php/php-src/blob/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 2018-12-25
  • 1970-01-01
相关资源
最近更新 更多