以下是【金聰采編】分享的內容全文:
先給大家介紹下php 與 nginx 的兩種處理方式,具體內容如下所示:
1.IP:Port 監聽方式
php-fpmdocker pull PHP:2.4-alpinenginx.conffastcgi_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 方式監聽
php-fpmlisten = /tmp/php-fpm.socknginx.conffastcgi_pass unix:/tmp/php-fpm.sock;
3.注意
php-fpm用ip:port方式建立鏈接,
nginx不要用unix socket方式建立鏈接,用ip:port方式建立連接就行
下面看下nginx與php-fpm通信的兩種方式
在linux中,nginx服務器和php-fpm可以通過tcp socket和unix socket兩種方式實現。
unix socket是一種終端,可以使同一臺操作系統上的兩個或多個進程進行數據通信。這種方式需要再nginx配置文件中填寫php-fpm的pid文件位置,效率要比tcp socket高。
tcp socket的優點是可以跨服務器,當nginx和php-fpm不在同一臺機器上時,只能使用這種方式。
windows系統只能使用tcp socket的通信方式
配置方法
tcp socket
tcp socket通信方式,需要在nginx配置文件中填寫php-fpm運行的ip地址和端口號。
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運行的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的運行端口號和socket文件的地址都是在php-fpm.conf中配置的。
php-fpm.conf文件在php安裝文件的/etc目錄下,
比如你的php安裝在/opt/php目錄,則應該是/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:9000listen = /var/run/php-fpm.sock
通過注釋可以看到,php-fpm的listen指令可以通過五種方式處理FastCGI請求,分別是:
1. ipv4:端口號
2. ipv6:端口號
3. port相當于 0.0.0.0:port,本機所有ipv4對應的端口號
4. [::]:port,包括ipv4和ipv6
5. unix socket文件
直接配置使用unix socket文件之后,會遇到access deny的問題,由于socket文件本質上還是一個文件,存在權限控制問題,默認由root用戶創建,因此nginx進程無權限訪問,應該配置如下命令:
; 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 0660listen.owner = wwwlisten.group = www listen.mode = 0660
可以配置nginx和php-fpm都是用www用戶,這樣就不會存在權限問題,當然也可以創建不同的用戶,然后加入同一個組,便于分配權限。
總結
以上所述是小編給大家介紹的php 與 nginx 的兩種處理方式及nginx與php-fpm通信的兩種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對金聰精品網站的支持!
1.軟件源碼推廣展示:目的展示軟件相關功能,接收技術學習者測試、測評;
2.教程課程信息展示:展示課程信息,傳授課程各階段內容;
3.設計素材圖片展示:展示素材設計理念、思維方式、傳播設計理念;
4.福利優惠信息展示:分享各類最新的福利信息,各種優惠信息展示;
以上分享目的僅供學習、參考使用,請勿用于其他用途,如果想商業使用或者代理,請自行聯系版權方獲取授權。任何未獲取授權的商業使用與本站無關,請自行承擔相應責任。
本站不存儲任何資源文件,敬請周知!
如果您認為本頁信息內容侵犯了您的相關權益(包含但不限于:著作權、首發權、隱私權等權利),或者您認為自己是此信息的權利人但是此信息不是自己發布的,可以直接版權舉報投訴,我們會根據網站注冊協議、資源分享協議等協議處理,以保護您的合法權益。
本網站采用 BY-NC-SA 協議進行授權 轉載請注明原文鏈接:php 與 nginx 的處理方式及nginx與php-fpm通信的兩種方式

侵權舉報/版權申訴



