当前位置: 首页 > 编程日记 > 正文

配置 php-fpm 监听的socket

一般现在我们配置的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 omittedserver ~ \.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 omittedserver ~ \.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

  • Previous message: unix domain sockets vs. internet sockets
  • Next message: unix domain sockets vs. internet sockets
  • Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

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.  Thismeans you can use UNIX file permissions to control access to communicatewith them.  I.e., you can limit what other processes can connect to thedaemon -- 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 offthe current system, so additional steps may have to be taken forsecurity.  On the other hand, you get network transparency.  With UNIXdomain sockets, you can actually retrieve the credential of the processthat 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-wireIP.  There is intentionally "no special knowledge" of the fact that theconnection is to the same system, so no effort is made to bypass thenormal IP stack mechanisms for performance reasons.  For example,transmission over TCP will always involve two context switches to get tothe remote socket, as you have to switch through the netisr, whichoccurs following the "loopback" of the packet through the syntheticloopback interface.  Likewise, you get all the overhead of ACKs, TCPflow control, encapsulation/decapsulation, etc.  Routing will beperformed in order to decide if the packets go to the localhost.Large sends will have to be broken down into MTU-size datagrams, whichalso adds overhead for large writes.  It's really TCP, it just goes overa loopback interface by virtue of a special address, or discovering thatthe address requested is served locally rather than over an ethernet(etc). - UNIX domain sockets have explicit knowledge that they're executing onthe same system.  They avoid the extra context switch through thenetisr, and a sending thread will write the stream or datagrams directlyinto the receiving socket buffer.  No checksums are calculated, noheaders are inserted, no routing is performed, etc.  Because they haveaccess to the remote socket buffer, they can also directly providefeedback to the sender when it is filling, or more importantly,emptying, rather than having the added overhead of explicitacknowledgement and window changes.  The one piece of functionality thatUNIX domain sockets don't provide that TCP does is out-of-band data.  Inpractice, 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
 

转载于:https://www.cnblogs.com/oxspirt/p/5109249.html

相关文章:

laravel基础课程---8、laravel响应和视图(响应是什么)

laravel基础课程---8、laravel响应和视图(响应是什么) 一、总结 一句话总结: 就是向请求返回的响应数据(一般为html(视图),当然也可以是变量值):所有的路由及控制器必须返…

ios应用内购买

参考: 1、http://troybrant.net/blog/2010/01/in-app-purchases-a-full-walkthrough/ 2、http://iosdeveloper.diandian.com/post/2011-08-26/4366441转载于:https://www.cnblogs.com/foxmin/archive/2012/09/08/2676580.html

mysql查询解析过程_MySQL查询执行过程详解

查询是用户通过设置某些查询条件,从表或其他查询中选取全部或者部分数据,以表的形式显示数据供用户浏览。查询是一个独立的、功能强大的、具有计算功能和条件检索功能的数据库对象。MySQL数据库中,MySQL查询同样是数据库的核心操作&#xff0…

.net erp(办公oa)开发平台架构之流程服务概要介绍

背景 搭建一个适合公司erp业务的开发平台。 架构概要图: 流程引擎开发平台: 包含流程引擎设计器,流程管理平台,流程引擎服务。目前只使用单个数据库进行管理。 流程引擎设计器 采用silverlight进行开发,本质是对流程…

数据分析-pca协方差

协方差是反映的变量之间的二阶统计特性,如果随机向量的不同分量之间的相关性很小,则所得的协方差矩阵几乎是一个对角矩阵。转载于:https://www.cnblogs.com/erweiyang/archive/2012/09/08/2676997.html

在javascript中判断类型

String 一个字符串始终是一个字符串,所以这一块是很容易。除非使用new(new String)调用,否则typeof将返回“object”。所以也要包含那些可以使用的字符串instanceof。 // Returns if a value is a string function isString (valu…

mysql中日期判断的函数_MySql判断汉字、日期、数字的函数

几个平常用的mysql函数 /***************************************************** 1.判断字符串是否为汉字 返回值:1-汉字 0-非汉字 *****************************************************/ DROP FUNCTION IF EXISTS fc_is_hanzi; CREATE FUNCTION fc_is_h几个平常…

c#.net调用pdf2swf.exe将pdf文件转换为swf,vs中运行正常,布署IIS服务器部署转换后文字部分为空白...

这个是权限问题, 需要在应用程序池中高级设置,将标识改为LocalSystem 转载于:https://www.cnblogs.com/shy1766IT/p/5114971.html

云计算开发要学习哪些东西?云计算开发的内容

云计算是一种基于互联网的计算方式,要实现云计算则需要一整套的技术架构去实施,包括网络、服务器、存储、虚拟化等等。 云计算目前分为公有云和私有云。两者的区别只是提供的服务的对象不同,一个是企业内部使用,一个则是面向公众。…

XHTML+CSS3(Chapter 1)

HTML5 Reference document: http://www.w3school.com.cn/html5/html_5_intro.asp 1. Use <video> to display some video in html (mp4. ogg.) <video src"movie.ogg" controls"controls"> </video> 2. Use <audio> to play …

oracle重建实例_记一次误删Oracle控制文件并恢复过程

概述当你在数据库运行时误删除了控制文件怎么办&#xff1f;很不幸有一次我就有这个情况,虽然是测试环境&#xff0c;这里因为我有事先把控制文件分别备份&#xff0c;所以恢复还是比较简单的。下面简单记录下怎么恢复。问题控制文件版本不一致一般是因为在实例运行时删除了控制…

成都Uber优步司机奖励政策(1月9日)

1月9日 奖励政策滴快车单单2.5倍&#xff0c;注册地址&#xff1a;http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单&#xff1a;http://www.cnblogs.com/mfryf/p/4612609.html 优步奖励低/不挣钱/怎么办?看这里&#xff1a;http://www.c…

vim-map

vim-map 软件版本&#xff1a;  ubuntu10.04  Linux version 2.6.32-42-generic  VIM - Vi IMproved 7.2目录&#xff1a; 1. 简介  2. 映射与运行模式的关系  3. 例子  4. 其他 1. 简介 如果想让 vim 成为你手中的利器&#xff0c;vim 的 map 功能就不得不学&…

Java并发面试,幸亏有点道行,不然又被忽悠了

2019独角兽企业重金招聘Python工程师标准>>> 前言 面试Java&#xff0c;必然要被问Java内存模型和Java并发开发。我被问到的时候&#xff0c;心里慌得一批&#xff0c;“额&#xff0c;是在《Thinking in Java》里面写的吗&#xff1f;果然每天增删改太low了” 要了…

mysql减少锁等待_降低锁竞争 减少MySQL用户等待时间

【IT168 技术】通过锁机制&#xff0c;可以实现多线程同时对某个表进行操作。如下图所示&#xff0c;在某个时刻&#xff0c;用户甲、用户乙、用户丙可能会同时或者先后(前面一个作业还没有完成)对数据表A进行查询或者更新的操作。当某个线程涉及到更新操作时&#xff0c;就需要…

UML中的六种关系的比较与学习

通过不断的学习并绘制UML图&#xff0c;整个画图的过程中深刻体会到其核心部分还是理解事物之间的关系&#xff0c;总结六大关系来深入学习&#xff0c;主要关系有六种&#xff1a;继承、实现、依赖、关联、聚合、组合。 区别于联系&#xff1a; 1.继承&#xff08;泛华&#x…

spark—3(Spark Scheduler)

2019独角兽企业重金招聘Python工程师标准>>> Spark的核心是根据RDD来实现的&#xff0c;Spark Scheduler则为Spark核心实现的重要一环&#xff0c;其作用就是任务调度。Spark的任务调度就是如何组织任务去处理RDD中每个分区的数据&#xff0c;根据RDD的依赖关系构建…

Tomcat手动配置简述【查询留存】

一、设置JAVA的环境变量JAVA_HOME 略 二、设置Tomcat的环境变量 一次性全配置了 Tomcat的根目录&#xff0c;例如【E:\tomcat-6.0.35】 创建下列环境变量&#xff1a; CATALINA_HOME: E:\tomcat-6.0.35 CATALINA_BASE: E:\tomcat-6.0.35 TOMCAT_HOME: E:\tomcat-6.0.35 然后修改…

linux mysql 不稳定_linux,mysql:今天写出一个十分弱智的bug!

今天写出一个十分弱智的bug&#xff0c;记录一下&#xff0c;提醒自己以后别这种犯错&#xff0c;不怕丢人哈~在写一个分页查询记录的sql时&#xff0c;要根据添加的时间逆序分页输出&#xff0c;之前的写法是酱紫&#xff1a;selectrecord.a, y.c from ( selecta,b from xorde…

IOS XML解析

<?xml version "1.0" encoding "utf-8"?> <video>小黄人</video> <video></video> <video/> <videos> <video> </video> </videos> 不能相互嵌套。 xml中的所有空格和空行都会当成字符来…

BAT架构师分享之:大型网站技术架构

早期的网站为了节省成本一般会设计成集中式系统&#xff0c;应用程序、数据库等都部署在一台服务器上。 但随着业务的快速度发展&#xff0c;逐渐出现瓶颈&#xff0c;按一定原则**&#xff08;应用拆分、服务拆分、数据拆分、应用解耦&#xff09;**&#xff0c;向分布式系统转…

mysql isreg_`Innodb` MySQL中如何优雅的删除大表跑路

最近很想写写MySQL相关的内容&#xff0c;就从这个话题出发吧有人说删MySQL表谁不会不就是drop table TABLENAME如果在生产环境中&#xff0c;你对一张TB级别的大表&#xff0c;敲下这行命令那么你的主管&#xff0c;大主管&#xff0c;隔壁的大主管 就会气势汹汹的冲向你其原因…

常用正则表达式集锦

链接地址&#xff1a;http://blog.csdn.net/tjcyjd/article/details/48416405 验证数字&#xff1a;^[0-9]*$验证n位的数字&#xff1a;^\d{n}$验证至少n位数字&#xff1a;^\d{n,}$验证m-n位的数字&#xff1a;^\d{m,n}$验证零和非零开头的数字&#xff1a;^(0|[1-9][0-9]*)$验…

C(第一个C程序) 和 C++ (第一个C++程序)对比碰撞

个人博客首页&#xff08;点击查看详情&#xff09; -- https://blog.51cto.com/11495268 1、简介 C 是对 C 的继承、扩展&#xff0c;但从语言角度来说&#xff0c;这是 两种变成语言&#xff0c;就一定存在不同&#xff0c;本文 就借助于 C、C 的 最精简标准程序 进行对比…

java的byte与C#的异同引起的字符处理问题。

java的byte是有符号类型(java就没有无符号类型的数据)&#xff0c;值域&#xff1a;-0128~127 c#的byte是无符号类型数值&#xff0c;值域&#xff1a;0~255 这在依赖字符编码处理程序中&#xff0c;两者源代码就不能通用了。 知道原因结局办法就容易多了。 1.使用&与运算 …

exec不同文件l怎么汇总_ABAQUS常见问题汇总 - 2.0版.doc

您所在位置&#xff1a;网站首页 > 海量文档&nbsp>&nbsp计算机&nbsp>&nbspC/C资料ABAQUS常见问题汇总 - 2.0版.doc154页本文档一共被下载&#xff1a;次,您可全文免费在线阅读后下载本文档。下载提示1.本站不保证该用户上传的文档完整性&#xff0c…

1月12号 UIView

UIView 1.为什么要UIView .可以用UIView作为容器&#xff0c;存放子视图 .管理事件UIEvent 2.ios坐标系 以左上角为坐标原点&#xff0c;向右边是x的正方向&#xff0c;向下是y的正向方 bounds: 相对于视图本身而言&#xff08;0&#xff0c;0&#xff0c;w, h&#xff09; fra…

小虎计算器-技术支持

2019独角兽企业重金招聘Python工程师标准>>> 最简单的计算器&#xff0c;包含历史记录 转载于:https://my.oschina.net/u/1405818/blog/3050764

jquery即时搜索查询插件jquery.search.js

jquery.search.js搜索插件是一款基于jquery的插件,任何一个input输入款均可即时转为查询框,可分为前台数据直接显示和后台传输数据显示两种方案! 文档说明:http://www.sameus.com 代码下载地址: http://code.google.com/p/17sameus/downloads/list 使用方式 jquery.search.js插…

node mysql 批量写入_请问如何使用node.js在MySQL中进行批量插入

catspeake我四处寻找关于批量插入对象的答案。Ragnar123的回答使我得出了这样的结论&#xff1a;function bulkInsert(connection, table, objectArray, callback) {let keys Object.keys(objectArray[0]);let values objectArray.map( obj > keys.map( key > obj[key]…