为了节省支出,公司需要将分布在不同机器的站点都迁移到一台机器,而目前不同机器运行的是不同的web服务,部分是nginx,部分是apache,由于牵涉较多rewrite规则,为了节省修改功夫,打算迁移后相应站点依然使用原来的web服务,但是同一台机器80端口只能分配给一种web服务使用,于是计划nginx使用80,apache使用其它端口例如8088,通过nginx反向代理,使apache的站点从80端口跳转到8088端口。
1、nginx和apache的安装过程略过
2、nginx的api.127.com站点反向配置
# cat api.127.com.conf
upstream api.127.com {
server 127.0.0.1:8088;
}
server{
listen 80;
server_name api.127.com;
location / {
root /home/web/api.127.com/nginx_proxy;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_pass http://api.127.com;
}
}
3、apache的api.127.com:8088站点配置
<VirtualHost *:8088>
ServerName api.127.com
ServerAlias api.127.com
DocumentRoot /home/web/api.127.com
DirectoryIndex index.html index.htm index.php
ErrorLog logs/api.127.com_error_log
CustomLog logs/api.127.com_access_log combined
<Directory /home/web/api.127.com>
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
<Directory ~ "^/home/web/api.127.com/(.+/)*\.svn">
Order allow,deny
Deny from all
</Directory>
4、最终实现的效果是浏览器打开地址api.127.com的时候实际是访问到api.127.com:8088