nginx設置多端口轉發(不同端口應用使用二級域名訪問).md
修改配置文件:nginx.conf
// 第一個應用配置
server {
listen 80 default;
server_name www.testwk.com;
index index.html index.htm index.jsp *;
root /alidata/www/default;
location ~ \.jsp$ {
proxy_pass http://127.0.0.1:8080;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log /alidata/log/nginx/access/default.log;
}
//第二個應用配置,這個應用不是運行在80端口上的。並且可以是另外一個服務器
//使用屬性proxy_pass設置
server {
listen 80;
server_name bak.testwk.com;
location / {
proxy_redirect off;
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:7878;
}
access_log /alidata/log/nginx/access/blog.log;
}
配置完成需要重啓nginx。 /etc/init.d/nginx restart
配置修改完成之後需要設置域名的解析設置,在解析設置中增加一個二級域名。比如:
我的主域名是:testwk.com,增加了一個二級域名爲bk.testwk.com, 然後結合nginx的配置實現,服務器上的一個運行在7878端口的應用可以以bk.testwk.com方式訪問,而且不需要指出端口就可以訪問(一般情況訪問需要加端口,比如bk.testwk.com:7878)。
注意:如果bk.testwk.com依然訪問的是testwk.com的情況時,可以在nginx.conf後面加上include vhost/*.conf;
重啓nginx;
./nginx -s reload 回車