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

 

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