nginx+php
nginx+php基礎架構
生產實踐
nginx配置文件:
主配置文件
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | [root@linux-node1?conf.d]#?cat?/etc/nginx/nginx.conf user?nginx; worker_processes?auto; error_log?/var/log/nginx/error.log; pid?/run/nginx.pid; #?Load?dynamic?modules.?See?/usr/share/nginx/README.dynamic. include?/usr/share/nginx/modules/*.conf; events?{ ????worker_connections?1024; } http?{ ????log_format??main??'$remote_addr?-?$remote_user?[$time_local]?"$request"?' ??????????????????????'$status?$body_bytes_sent?"$http_referer"?' ??????????????????????'"$http_user_agent"?"$http_x_forwarded_for"'; ????access_log??/var/log/nginx/access.log??main; ????sendfile????????????on; ????tcp_nopush??????????on; ????tcp_nodelay?????????on; ????keepalive_timeout???65; ????types_hash_max_size?2048; ????include?????????????/etc/nginx/mime.types; ????default_type????????application/octet-stream; ????include?/etc/nginx/conf.d/*.conf; ????} [root@linux-node1?conf.d]# |
include配置文件
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | [root@linux-node1?conf.d]#?cat?bbs.conf? server?{ ????????listen???????80; ????????server_name??bbs.sense.com; ????????access_log??/tmp/bbs.log??main; ????????location?/?{ ????????????root???/www/bbs; ????????????index??index.html?index.htm; ????????} ????????location?~?.*\.(php|php5)?$?{ ????????????root?/www/bbs; ????????????fastcgi_pass??192.168.56.14:9000;?? ????????????fastcgi_index?index.php; ????????????include?fastcgi.conf; ????????} ????} [root@linux-node1?conf.d]#?cat??www.conf? server?{ ????????listen???????80; ????????server_name?www.sense.com; ????????access_log??/tmp/www.log??main; ????????location?/?{ ????????????root???/www/www; ????????????index??index.html?index.htm; ????????} ????????location?~?.*\.(php|php5)?$?{ ????????????root?/www/www; ????????????fastcgi_pass??192.168.56.12:9000; ????????????fastcgi_index?index.php; ????????????include?fastcgi.conf; ????????} ????} [root@linux-node1?conf.d]# |
2.后臺php需要開啟900端口,也就是說要編譯或者安裝php,php的監聽的地址修改為本機的ip地址,不能在是127.0.0.1了 否則nginx無法代理
3.nginx每個項目代碼需要保存一份,php后臺每個項目需要保存一份,代碼目錄要一致
靜態的html用戶請求 請求的是nginx上面的代碼,動態的請求,請求的php服務器上面的代碼
舉一個簡單的例子:bbs.sense.com 的代碼在nginx上面/www/bbs 下保存一份,也得在php所在的服務器上面的/www/bbs 下面保存一份,靜態的請求請求nginx服務器的代碼,動態的請求請求php服務器的代碼
上面基礎架構存在問題:nginx單點? php單點? nginx單點可以用keepalived解決,但是php單點必須得增加后端服務器,如果增加后端的服務器,那么nginx應該怎么配置呢
第二種架構
nginx主配置文件
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | [root@linux-node1?www]#?cat?/etc/nginx/nginx.conf user?nginx; worker_processes?auto; error_log?/var/log/nginx/error.log; pid?/run/nginx.pid; #?Load?dynamic?modules.?See?/usr/share/nginx/README.dynamic. include?/usr/share/nginx/modules/*.conf; events?{ ????worker_connections?1024; } http?{ ????log_format??main??'$remote_addr?-?$remote_user?[$time_local]?"$request"?' ??????????????????????'$status?$body_bytes_sent?"$http_referer"?' ??????????????????????'"$http_user_agent"?"$http_x_forwarded_for"'; ????access_log??/var/log/nginx/access.log??main; ????sendfile????????????on; ????tcp_nopush??????????on; ????tcp_nodelay?????????on; ????keepalive_timeout???65; ????types_hash_max_size?2048; ????include?????????????/etc/nginx/mime.types; ????default_type????????application/octet-stream; ?????upstream??fastcgiserver?{ ?????server?192.168.56.12:9000;??##此時是同一代碼 ?????server?192.168.56.14:9000;??##此時是同一代碼 ???} ????include?/etc/nginx/conf.d/*.conf; ????} [root@linux-node1?www]# |
nginx include配置文件
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@linux-node1?conf.d]#?cat?www.conf? server?{ ????????listen???????80; ????????server_name?www.sense.com; ????????access_log??/tmp/www.log??main; ????????location?/?{ ????????????root???/www/www; ????????????index??index.html?index.htm; ????????} ????????location?~?.*\.(php|php5)?$?{ ????????????root?/www/www; ????????????fastcgi_pass??fastcgiserver;??#此處有修改 ????????????fastcgi_index?index.php; ????????????include?fastcgi.conf; ????????} ????} [root@linux-node1?conf.d]# |
這樣做是輪詢的方式從后端的php日志依然能夠得出來,靜態文件還是請求nginx動態文件還是請求php服務器,nginx和后端的php服務器組依然要部署和nginx相同路徑的代碼
本文轉自 小小三郎1 51CTO博客,原文鏈接:http://blog.51cto.com/wsxxsl/1975109,如需轉載請自行聯系原作者
總結
- 上一篇: 阿里云云计算ACP考试知识点(标红为重点
- 下一篇: LeetCode:Minimum Pat