Nginx实现HTTP反向代理配置(代理到不同的URI上)
浏览数 156993
赞
(0)
这里实现浏览器访问
http://www.yiluphp.com/jim/page/666
被反向代理到(实际访问的):
http://user.yiluphp.com/user/blog/jim/page/666
nginx的虚拟主机配置如下
这个是用户直接访问的域名虚拟主机
upstream teststream{
server 127.0.0.1:8088;}
server {
listen 80;
server_name www.yiluphp.com;
root /data/web/www.yiluphp.com;
index index.html;
access_log /logs/www.yiluphp.com.access.log;
error_log /logs/www.yiluphp.com.error.log;
location / {
# 如果找不到真实存在的文件,把请求分发至反向代理服务器
try_files $uri $uri/ @testproxy;
}
location @testproxy{
proxy_pass http://teststream; #使用反向代理池,也可以直接写IP,我尝试完写本机的域名,好像还是会自动解析成IP再请求
proxy_redirect ~^http://$host(.*) http://$host$1;
proxy_set_header Host user.yiluphp.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
proxy_cookie_domain domino.server nginx.server;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
#更多有关反向代理的配置项请百度
}location ~ /\.(ht|svn|git) {
deny all;
}
#BEGIN php-fpm
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
#END php-fpm
}
这个是反正代理到的虚拟主机,即真正被访问的虚拟主机。
因为反向代理使用IP加端口访问,所以新建了一个虚拟主机,不用80端口,80端口被太多域名使用了,以免冲突。
server {
listen 8088;
server_name 127.0.0.1;
root /data/web/user.yiluphp.com;
index index.php;
access_log /logs/user.yiluphp.com.access.log main;
error_log /logs/user.yiluphp.com.error.log;
location ~ /\.(ht|svn|git) {
deny all;
}
#BEGIN php-fpm
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params; #这里引入一个定义了一些nginx环境变量的文件
fastcgi_param REQUEST_URI /user/blog$request_uri; #这里重新定义请求路径,对laravel之类的路由有用,其它的框架没测试
}
#END php-fpm
}
listen 8088;
server_name 127.0.0.1;
root /data/web/user.yiluphp.com;
index index.php;
access_log /logs/user.yiluphp.com.access.log main;
error_log /logs/user.yiluphp.com.error.log;
location ~ /\.(ht|svn|git) {
deny all;
}
#BEGIN php-fpm
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params; #这里引入一个定义了一些nginx环境变量的文件
fastcgi_param REQUEST_URI /user/blog$request_uri; #这里重新定义请求路径,对laravel之类的路由有用,其它的框架没测试
}
#END php-fpm
}
重启nginx即可使用。