Linux -- 學習 nginx 配置

1、反向代理 tomcat 8080

server {
    listen  80;

    # 1.設置 ip 地址
    server_name  192.168.23.131;
    
    location / {
        root html;
        # 2.設置轉發地址
        proxy_pass http://127.0.0.1:8080;
        index  index.html index.htm;
    }
}

2、反向代理多個 tomcat

# 1.兩個 tomcat 分別啓動在 8080 和 8081 端口
# 2.8080 端口下有 edu 文件夾,下面有 index.html 文件
# 3.8081 端口下有 vod 文件夾,下面有 index.html 文件

# server 配置
server {
    # 1.監聽 9001 端口
    listen       9001;
    server_name       192.168.23.131;

    location ~ /edu/ {
        proxy_pass http://127.0.0.1:8080;
    }

    location ~ /vod/ {
        proxy_pass http://127.0.0.1:8081;
    }
}

說明:

1、~ 表示 uri 包含正則表達式,並且區分大小寫

2、~* 表示 uri 包含正則表達式,並且不區分大小寫

3、負載均衡

# 1.兩個 tomcat 分別啓動在 8080 和 8081 端口
# 2.8080 端口下有 edu 文件夾,下面有 index.html 文件,內容爲 8080
# 3.8081 端口下有 edu 文件夾,下面有 index.html 文件,內容爲 8081

# server 配置
# weight 爲權重,可選配置
upstream myserver {
    server  127.0.0.1:8080 weight=1;
    server  127.0.0.1:8081 weight=2;
}

server {
    # 1.監聽 9001 端口
    listen       9001;
    server_name       192.168.23.131;

    location ~ /edu/ {
        proxy_pass http://myserver ;
    }

}

4、動靜分離

# 根目錄下創建文件夾 /data/www 和 /data/image
# www 目錄下放置 index.html 文件
# image 目錄下放置 001.jpg 文件

# server 配置
server {
    # 1.監聽 9001 端口
    listen       9001;
    server_name       192.168.23.131;

    location /www/ {
        root /data/;
        index  index.html index.htm;
    }

    location /image/ {
        root /data/;
        # 開啓目錄瀏覽,可選配置
        autoindex on;
    }

}

# 訪問文件
http://192.168.23.131:9001/www/index.html

# 訪問圖片
http://192.168.23.131:9001/image/001.jpg

 

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