Nginx + php 目前有兩種方式
1.nginx + apache ?
? ? ? ? nginx 負責靜態內容、反向代理和保持連接,apache則負責處理動態內容。
DocumentRoot /var/www/html/
# Define the virtual hos?t
1.nginx + apache ?
? ? ? ? nginx 負責靜態內容、反向代理和保持連接,apache則負責處理動態內容。
2.nginx + fastcgi php-fpm
? ? ? ??
? ? ? ??
一、nginx + apache?
采用Nginx前端Apache后端的服務器架構,這樣可以很好地結合了Nginx高并發和靜態頁面高效率以及Apache穩定的動態頁面處理特點,再也不用擔心Nginx以FastCGI模式運行PHP時的502問題和Apache處理靜態頁面過慢、負載過高的問題。
這里不再重復nginx和apache 的安裝步驟,直接上配置文件。
?
apache部分:
修改監聽端口 ? ?
? ? ? ? ? ? ? ?Listen 8080
? ? ? ? ? ? ? ?Listen 8080
增加虛擬主機
?NameVirtualHost 127.0.0.1:8080
?NameVirtualHost 127.0.0.1:8080
?
# Define Server document rootDocumentRoot /var/www/html/
# Define the virtual hos?t
<VirtualHost 127.0.0.1:8080>
ServerName www.xxx.com
ServerAlias xx.com
DocumentRoot /var/www/html/
<Directory "/var/www/html/">
ServerName www.xxx.com
ServerAlias xx.com
DocumentRoot /var/www/html/
<Directory "/var/www/html/">
Options FollowSymLinks -Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
</VirtualHost>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
</VirtualHost>
nginx 部分:
修改配置文件
? ??? location / {
? ??? location / {
root /var/www/html; #apache的網站根目錄index index.php index.html index.htm;}
#將php文件請求分發給后端的apachelocation ~ \.php$ { proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://127.0.0.1:8080;}
?#最簡方式 #location ~ \.php$ { ?# ? ?proxy_pass ? http://127.0.0.1; ?#}
#將圖像和靜態文件由nginx處理location ~ \.*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid| doc|ppt|pdf|xls|mp3|wma)$ {root /var/www/html;expires 15d; 更新時間 15天}#將js文件由nginx處理 location ~ \.*.(js|css)$ {expires 1h; 更新時間 1小時
}
?# 如果你需要客戶端緩存的內容以及媒體,圖片等文件固定放置一些目錄下的話,就把上面那個
? ? ? ??# location注釋掉,用下面這種方式
? ? ? ??# location ~ ^/(images|javascript|js|css|flash|media|static)/? {
? ? ? ??#? expires 2d;
? ? ? ??#}
? ? ? ??# location注釋掉,用下面這種方式
? ? ? ??# location ~ ^/(images|javascript|js|css|flash|media|static)/? {
? ? ? ??#? expires 2d;
? ? ? ??#}