2019-08-22 10:49:52 4034瀏覽
本篇文章扣丁學(xué)堂php培訓(xùn)小編主要是給讀者們分享一下php與nginx的處理方式及nginx與php-fpm通信的兩種方式,廢話不多說(shuō)了,下面就一起來(lái)了解一下吧。
先給大家介紹下php與nginx 的兩種處理方式,具體內(nèi)容如下所示:
1、IP:Port 監(jiān)聽(tīng)方式
php-fpm docker pull PHP:2.4-alpine nginx.conf fastcgi_pass 127.0.0.1:9000;
php-fpm 在容器里的 nginx.conf
location /php { proxy_set_header Host $host:$server_port; proxy_pass http://138.38.38.111:80/; }
2、UDS 方式監(jiān)聽(tīng)
php-fpm listen = /tmp/php-fpm.sock nginx.conf fastcgi_pass unix:/tmp/php-fpm.sock;
3、注意
php-fpm用ip:port方式建立鏈接,
nginx不要用unix socket方式建立鏈接,用ip:port方式建立連接就行
下面看下nginx與php-fpm通信的兩種方式
在linux中,nginx服務(wù)器和php-fpm可以通過(guò)tcp socket和unix socket兩種方式實(shí)現(xiàn)。
unix socket是一種終端,可以使同一臺(tái)操作系統(tǒng)上的兩個(gè)或多個(gè)進(jìn)程進(jìn)行數(shù)據(jù)通信。這種方式需要再nginx配置文件中填寫php-fpm的pid文件位置,效率要比tcp socket高。
tcp socket的優(yōu)點(diǎn)是可以跨服務(wù)器,當(dāng)nginx和php-fpm不在同一臺(tái)機(jī)器上時(shí),只能使用這種方式。
windows系統(tǒng)只能使用tcp socket的通信方式。
配置方法
tcp socket
tcp socket通信方式,需要在nginx配置文件中填寫php-fpm運(yùn)行的ip地址和端口號(hào)。
location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; }
unix socket
unix socket通信方式,需要在nginx配置文件中填寫php-fpm運(yùn)行的pid文件地址。
location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; }
php-fpm的運(yùn)行端口號(hào)和socket文件的地址都是在php-fpm.conf中配置的。
php-fpm.conf文件在php安裝文件的/etc目錄下,
比如你的php安裝在/opt/php目錄,則應(yīng)該是/opt/php/php-fpm.conf。
; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; 'port' - to listen on a TCP socket to all IPv4 addresses on a ; specific port; ; '[::]:port' - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = 127.0.0.1:9000 listen = /var/run/php-fpm.sock
通過(guò)注釋可以看到,php-fpm的listen指令可以通過(guò)五種方式處理FastCGI請(qǐng)求,分別是:
1. ipv4:端口號(hào)
2. ipv6:端口號(hào)
3. port相當(dāng)于 0.0.0.0:port,本機(jī)所有ipv4對(duì)應(yīng)的端口號(hào)
4. [::]:port,包括ipv4和ipv6
5. unix socket文件
直接配置使用unix socket文件之后,會(huì)遇到access deny的問(wèn)題,由于socket文件本質(zhì)上還是一個(gè)文件,存在權(quán)限控制問(wèn)題,默認(rèn)由root用戶創(chuàng)建,因此nginx進(jìn)程無(wú)權(quán)限訪問(wèn),應(yīng)該配置如下命令:
; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. Many ; BSD-derived systems allow connections regardless of permissions. ; Default Values: user and group are set as the running user ; mode is set to 0660 listen.owner = www listen.group = www listen.mode = 0660
可以配置nginx和php-fpm都是用www用戶,這樣就不會(huì)存在權(quán)限問(wèn)題,當(dāng)然也可以創(chuàng)建不同的用戶,然后加入同一個(gè)組,便于分配權(quán)限。
想要了解更多關(guān)于PHP開(kāi)發(fā)方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂PHP培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的PHP講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開(kāi)發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂PHP技術(shù)交流群:374332265。
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開(kāi)發(fā)免費(fèi)公開(kāi)課】
查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>