一般现在我们配置的PHP的web环境,如LNMP(linux+Nginx+Mysql+PHP), 这里linux可能是centos, ubuntu..., 数据库可能是mysql, postgresql, sql server等。。

在服务器上安装PHP-FPM, nginx后, 我们要配置Nginx的http模块, 让 .php的文件由nginx 转发给PHP-FPM处理,然后在将php-fpm的处理结果通过http响应传给浏览器,就完成了一次http的请求。。

在配置 Nginx 的http模块的时候, 通常是这样:

server ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
}
也可以这样,
server ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
那么这两种方式有什么区别呢?? 这就是我这篇博文所要解释的问题。下面,我带大家来分析一下其中的原理,一下是我的一些理解,不对的地方还请大家不吝赐教,我将很感激~~

PHP-FPM can listen on multiple sockets. I also listen on Unix sockets, or TCP sockets. See how this works and how to ensure Nginx is properly sending requests to PHP-FPM.

Command Rundown

Default Configuration

Edit PHP-FPM configuration

# Configure PHP-FPM default resource pool
sudo vim /etc/php5/fpm/pool.d/www.conf

PHP-FPM Listen configuration:

# Stuff omitted
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data

Also edit Nginx and see where it's sending request to PHP-FPM:

# Files: /etc/nginx/sites-available/default

# ... stuff omitted

server ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}

We can see above that Nginx is sending requests to PHP-FPM via a unix socket (faux file) at /var/run/php5-fpm.sock. This is also where the www.conf file is setting PHP-FPM to listen for connections.

Unix Sockets

These are secure in that they are file-based and can't be read by remote servers. We can further use linux permission to set who can read and write to this socket file.

Nginx is run as user/group www-data. PHP-FPM's unix socket therefore needs to be readable/writable by this user.

If we change the Unix socket owner to user/group ubuntu, Nginx will then return a bad gateway error, as it can no longer communicate to the socket file. We would have to change Nginx to run as user "ubuntu" as well, or set the socket file to allow "other" (non user nor group) to be read/written to, which is insecure.

# Stuff omitted
listen = /var/run/php5-fpm.sock
listen.owner = ubuntu
listen.group = ubuntu

So, file permissions are the security mechanism for PHP-FPM when using a unix socket. The faux-file's user/group and it's user/group/other permissions determines what local users and processes and read and write to the PHP-FPM socket.

TCP Sockets

Setting the Listen directive to a TCP socket (ip address and port) makes PHP-FPM listen over the network rather than as a unix socket. This makes PHP-FPM able to be listened to by remote servers (or still locally over the localhost network).

Change Listen to Listen 127.0.0.1:9000 to make PHP-FPM listen on the localhost network. For security, we can use thelisten.allowed_clients rather than set the owner/group of the socket.

PHP-FPM:

# Listen on localhost port 9000
Listen 127.0.0.1:9000
# Ensure only localhost can connect to PHP-FPM
listen.allowed_clients = 127.0.0.1

Nginx:

# Files: /etc/nginx/sites-available/default

# ... stuff omitted

server ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
} http://lists.freebsd.org/pipermail/freebsd-performance/2005-February/001143.html

unix domain sockets vs. internet sockets

Robert Watson rwatson at FreeBSD.org
Fri Feb 25 02:29:14 PST 2005

On Fri, 25 Feb 2005, Baris Simsek wrote:

> I am coding a daemon program. I am not sure about which type of sockets
> i should use. Could you compare ip sockets and unix domain sockets? My
> main criterions are performance and protocol load. What are the
> differences between impelementations of them at kernel level?

There are a few differences that might be of interest, in addition to the
already pointed out difference that if you start out using IP sockets, you
don't have to migrate to them later when you want inter-machine
connectivity: - UNIX domain sockets use the file system as the address name space. This
means you can use UNIX file permissions to control access to communicate
with them. I.e., you can limit what other processes can connect to the
daemon -- maybe one user can, but the web server can't, or the like.
With IP sockets, the ability to connect to your daemon is exposed off
the current system, so additional steps may have to be taken for
security. On the other hand, you get network transparency. With UNIX
domain sockets, you can actually retrieve the credential of the process
that created the remote socket, and use that for access control also,
which can be quite convenient on multi-user systems. - IP sockets over localhost are basically looped back network on-the-wire
IP. There is intentionally "no special knowledge" of the fact that the
connection is to the same system, so no effort is made to bypass the
normal IP stack mechanisms for performance reasons. For example,
transmission over TCP will always involve two context switches to get to
the remote socket, as you have to switch through the netisr, which
occurs following the "loopback" of the packet through the synthetic
loopback interface. Likewise, you get all the overhead of ACKs, TCP
flow control, encapsulation/decapsulation, etc. Routing will be
performed in order to decide if the packets go to the localhost.
Large sends will have to be broken down into MTU-size datagrams, which
also adds overhead for large writes. It's really TCP, it just goes over
a loopback interface by virtue of a special address, or discovering that
the address requested is served locally rather than over an ethernet
(etc). - UNIX domain sockets have explicit knowledge that they're executing on
the same system. They avoid the extra context switch through the
netisr, and a sending thread will write the stream or datagrams directly
into the receiving socket buffer. No checksums are calculated, no
headers are inserted, no routing is performed, etc. Because they have
access to the remote socket buffer, they can also directly provide
feedback to the sender when it is filling, or more importantly,
emptying, rather than having the added overhead of explicit
acknowledgement and window changes. The one piece of functionality that
UNIX domain sockets don't provide that TCP does is out-of-band data. In
practice, this is an issue for almost noone. In general, the argument for implementing over TCP is that it gives you
location independence and immediate portability -- you can move the client
or the daemon, update an address, and it will "just work". The sockets
layer provides a reasonable abstraction of communications services, so
it's not hard to write an application so that the connection/binding
portion knows about TCP and UNIX domain sockets, and all the rest just
uses the socket it's given. So if you're looking for performance locally,
I think UNIX domain sockets probably best meet your need. Many people
will code to TCP anyway because performance is often less critical, and
the network portability benefit is substantial. Right now, the UNIX domain socket code is covered by a subsystem lock; I
have a version that used more fine-grain locking, but have not yet
evaluated the performance impact of those changes. I've you're running in
an SMP environment with four processors, it could be that those changes
might positively impact performance, so if you'd like the patches, let me
know. Right now they're on my schedule to start testing, but not on the
path for inclusion in FreeBSD 5.4. The primary benefit of greater
granularity would be if you had many pairs of threads/processes
communicating across processors using UNIX domain sockets, and as a result
there was substantial contention on the UNIX domain socket subsystem lock.
The patches don't increase the cost of normal send/receive operations, but
due add extra mutex operations in the listen/accept/connect/bind paths. Robert N M Watson
 

配置 php-fpm 监听的socket的更多相关文章

  1. Oracle一个实例配置多个监听

    要想给一个Oracle实例配置多个监听,首先要定义多个监听器,因为是多个监听,势必会有一些监听端口不是1521. 现在服务端的listener.ora文件中定义如下监听器: LISTENER = (D ...

  2. 黄聪:windows下使用xampp3.2.2配置多个监听端口和不同的网站目录

    windows下使用xampp3.2.2配置多个监听端口和不同的网站目录 一:配置Apache文件httpd.conf 打开Apache的配置文件httpd.conf,可以通过点击xampp的Apac ...

  3. 【PHP】xampp配置多个监听端口和不同的网站目录(转)

    转自:https://blog.csdn.net/cc1314_/article/details/75646344 windows下使用xampp配置多个监听端口和不同的网站目录 一:配置Apache ...

  4. 部署grafana+telegraf+influxdb 及 配置 jmeter后端监听

    搞性能测试,可以搭建Grafana+Telegraf+InfluxDB 监控平台,监控服务器资源使用率.jmeter性能测试结果等. telegraf: 是一个用 Go 编写的代理程序,可收集系统和服 ...

  5. Apache配置多个监听端口

    以前做PC上的,都是配置一个端口,整一大堆的虚拟目录: 在 \conf\extra下找到httpd-vhosts.conf这个配置文件,想下面这样配置就行,监听80端口,访问相应的ServerName ...

  6. Apache配置多个监听端口和不同的网站目录的简单方法(转)

    转自http://www.waaqi.com/archives/707.html 由于开发的多项目,每个项目又要独立,要用根目录地址. 所以这时候我们需要配置多个不同目录的Apache,如果是外部网可 ...

  7. Apache无法正常启动(配置多个监听端口)

    Apache监测多个端口配置: 1.conf->extra->httpd-vhosts.conf  检查配置项是否写错 2.http.conf listen端口是否监听正确 3.环境变量中 ...

  8. xampp配置多个监听端口和不同的网站目录

    1.配置Apache文件httpd.conf 打开xampp安装目录下的Apache->conf文件夹下的httpd.conf,用记事本打开 首先在Listen 80端口下添加其他监听端口: L ...

  9. apache主机(网站)配置,port监听,文件夹訪问权限及分布式权限

    前言 一个网站的两个核心信息为: 主机名称(server名/网站名):ServerName server名 网站位置(网站文件夹路径):DocumentRoot "实际物理路径" ...

  10. Nginx配置IPv6端口监听及务器设置IPV6及Https支持并通过AppStore审核

    一.监听端口 从Nginx 1.3的某个版本起,默认ipv6only是打开的,所以,我们只需要在监听中加入ipv6监听即可,不过推荐都手动加上比较好,代码如下: listen [::]: ipv6on ...

随机推荐

  1. TCP/IP,http,socket,长连接,短连接——小结。

    来源:http://blog.chinaunix.net/uid-9622484-id-3392992.html TCP/IP是什么? TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. ...

  2. json 帮助工具

    import java.lang.reflect.Type; import com.google.gson.Gson; /** * json 帮助工具 */public final class Gso ...

  3. linux下mysql root密码忘记修改方法

    一.MySQL密码的恢复方法之一 如果忘记了MySQL的root密码,可以用以下方法重新设置:1.切换到root下su root 2. KILL掉系统里的MySQL进程: killall -TERM ...

  4. php 使用操作符

    算术操作符.字符串操作符.赋值操作符 1.比较操作符 比较运算符 例子 名称 结果 $a == $b 等于 TRUE,如果类型转换后 $a 等于 $b. $a === $b 全等 TRUE,如果 $a ...

  5. TortoiseGit保存用户名密码的方法

    方法一: 设置 -> git 编辑本地 .git/config 增加 1 [credential]    2     helper = store 保存,输入一次密码后第二次就会记住密码了 方法 ...

  6. linux学习的哲学层面的思考-架构

    参考:http://blog.chinaunix.net/uid-26119273-id-3356414.html 学习Linux,准备做产品的话,不要把Linux当成了终极目标(当然,这是对应用而言 ...

  7. Hibernate 系列教程4-单向多对一

    项目图片 hibernate.cfg.xml <mapping resource="com/jege/hibernate/one/way/manytoone/User.hbm.xml& ...

  8. 8633 回文划分(dp)

    8633 回文划分 该题有题解 时间限制:1000MS  内存限制:1000K提交次数:169 通过次数:63 题型: 编程题   语言: G++;GCC Description 我们说一个字符串是回 ...

  9. The Accumulation of Capital

    The Accumulation of Capital ---- by *Adam Smith Capitals are increased by parsimony, and diminished ...

  10. Bootstrap 模态对话框只加载一次 remote 数据的解决办法 转载

    http://my.oschina.net/qczhang/blog/190215 摘要 前端框架 Bootstrap 的模态对话框,可以使用 remote 选项指定一个 URL,这样对话框在第一次弹 ...