Nginx配置共用80端口|端口轉發端口映射

problem

1、http默認端口爲80
2、一臺服務器有多個服務,都想監聽80
3、有些服務限用指定端口,修改麻煩
4、外網訪問域名時要加端口,很麻煩

solution

1、先在兩個空閒的端口上分別部署項目(非80,假設是81和82)

// nginx.conf
# test1項目配置
server {
    listen       81;
    root         /www/wwwroot/test1;
    index        index.html;
    location / {}
}

# test2項目配置
server {
    listen       82;
    root         /www/wwwroot/test2;
    index        index.html;
    location / {}
}

2、內網進行端口轉發(監聽80端口)

// nginx.conf
# nginx 81端口配置 (監聽test1二級域名)
server {
    listen  81;
    server_name     test1.gwj1314.space;
    location / {
        proxy_pass      http://localhost:81; # 轉發
    }
}

# nginx 82端口配置 (監聽test2二級域名)
server {
    listen  82;
    server_name     test2.gwj1314.space;
    location / {
        proxy_pass      http://localhost:82; # 轉發
    }
}

example

實際轉發的時候,記得加上JavaScript,css和圖片等元素的轉發。

server
{
    listen 80;
    server_name ftp.gwj1314.space;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/chfs;
    
   	location / {
        proxy_pass      http://localhost:81; # 轉發
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    	proxy_pass      http://localhost:81; # 轉發
        expires      30d;
        error_log off;
        access_log /dev/null;
    }
    
    location ~ .*\.(js|css)?$
    {
    	proxy_pass      http://localhost:81; # 轉發
        expires      12h;
        error_log off;
        access_log /dev/null; 
    }

}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章