这是nginx本来就提供的功能.
应用场景
我只有一台vps
有多个域名
对外部要都用80提供服务
前置条件
用不同的端口,把不同的http服务都启动起来.
除了80要留给nginx,其他随便用
配置nginx
sudo vim /etc/nginx/nginx.conf
加入或者修改为
server {
listen 80;
server_name www.111cn.net;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
这样从www.111cn.net访问过来的,就会反向代理到8000的http服务上
server {
listen 80;
server_name 111cn.net;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
server {
listen 80;
server_name www.111cn.net;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
改成这样,111cn.net过来的,就会代理到8080端口.
某些情况下需要完全的代理:
location / {
proxy_pass http://127.0.0.1:8008;
proxy_set_header X-Real-IP $remote_addr; # http://wiki.nginx.org/HttpProxyModule
proxy_set_header Host $host; # pass the host header - http://wiki.nginx.org/HttpProxyModule#proxy_pass
proxy_http_version 1.1; # recommended with keepalive connections - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}