php-fpm 使用上游和位置有什么区别?

Van*_*yen 21 linux http configuration nginx php-fpm

我一直在四处寻找,但找不到直接的答案,如果有人可以澄清这一点,将不胜感激,谢谢!

location ~ \.php$ {
    try_files      $uri = 404;
    fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index  index.php;
    include        fastcgi.conf;
}
Run Code Online (Sandbox Code Playgroud)

或/与?

upstream php {
    server         unix:/run/php-fpm/php-fpm.sock;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

eta*_*klo 30

location 用于匹配表达式并为它们创建规则。

upstream 定义可以引用的服务器。

在你的例子中,这意味着如果你想获得一个等价物

location ~ \.php$ {
    try_files      $uri = 404;
    fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index  index.php;
    include        fastcgi.conf;
}
Run Code Online (Sandbox Code Playgroud)

,你需要

upstream php {
    server         unix:/run/php-fpm/php-fpm.sock;
}
location ~ \.php$ {
    try_files      $uri = 404;
    fastcgi_pass   php;
    fastcgi_index  index.php;
    include        fastcgi.conf;
}
Run Code Online (Sandbox Code Playgroud)

上游块的好处是您可以将多个服务器/端口/服务配置为上游并在它们上分配流量,例如:

upstream php {
    server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
    server 192.68.1.2     weight=5;
    server         unix:/run/php-fpm/php-fpm.sock;
}
Run Code Online (Sandbox Code Playgroud)

您可以在 nginx 文档中找到有关此内容的更多信息:

http://nginx.org/en/docs/http/ngx_http_upstream_module.html


小智 9

我发现,nginx至少从1.6.2 开始,位置块的工作语法是:

location ~ \.php$ {
    try_files      $uri = 404;
    fastcgi_pass   php;
    fastcgi_index  index.php;
    include        fastcgi.conf;
}
Run Code Online (Sandbox Code Playgroud)

即:http://在引用 php 后端之前不应指定协议。该http://php语法与使用proxy_pass指令,不fastcgi_pass