本文就是實現Nginx作為前端,apache作為后端。當用戶訪問的是80端口的nginx,然后nginx將靜態內容留給自己,其余的轉發給非80端口的apache,apache處理完畢后再回傳給nginx。
1.修改(或者添加)nginx配置(vhost),本案例在nginx/conf/vhosts里面創建文件test.conf
#cat /usr/local/nginx/conf/vhosts/test.conf
server {
listen 80;
server_name 項目域名;
root /home/wwwroot/test;
index index.html index.php ;
error_page 404 @proxy;
include proxy-pass-php.conf;
}
以上配置中的proxy-pass-php.conf,在nginx的conf文件夾中,proxy-pass-php.conf內容如下:
location /
{
try_files $uri @apache;
}
location @apache
{
internal;
proxy_pass http://127.0.0.1:88;
include proxy.conf;
}
location ~ [^/]\.php(/|$)
{
proxy_pass http://127.0.0.1:88;
include proxy.conf;
}
# nginx找不到文件時,轉發請求給后端Apache
location @proxy {
proxy_set_header Host $http_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:88;
}
2.nginx配置完畢,接下來就要配置apache了,首先確定apache的端口與上面配置的一致(上面配置了88端口是apache的),找到apache/conf/httpd.conf這個文件,修改監聽端口:
Listen 127.0.0.1:88
3.配置apache的vhost,該目錄一般在apache/conf/vhost,本案例在apache/conf/vhost創建test.conf,內容如下:
<VirtualHost *:88>
ServerAdmin admin@email.com
#php_admin_value open_basedir "/ home / wwwroot:/ tmp /:/ var / tmp /:/ proc /"
DocumentRoot /home/wwwroot/test
ServerName 項目域名
<Directory /home/wwwroot/test/>
allow from all
</Directory>
</Virtualhost>
4.重啟ngnix和apache
service ngnix restar service httpd restar
