docker-componse nginx 反向代理

docker-componse nginx 反向代理

工程目录

xiao@z:/opt/soft/lib/dc/nginx$ tree
.
├── conf
│   └── nginx.conf
├── conf.d
├── docker-compose.yml
├── html
│   ├── 50x.html
│   └── index.html
├── logs
│   ├── access.log
│   └── error.log
nginx.conf
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  300;
    server {
        listen       8080;  ## 这里的监听端口 8080 要和docker-compose.yml 中 
        					## port:  8080:8080(这两个一致),不然反向代理失败 !!
        server_name  localhost;

      
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }


        location /wx/proxy/ {
                port_in_redirect on;
                proxy_pass http://192.168.0.104:9999/;
                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;
        }

       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
docker-compose.yml 配置文件
version: '3'
services:
  nginx:
    image: nginx:stable-alpine-perl
    container_name: nginx
    ports:
      - 8080:8080
    volumes:
    - $PWD/html:/usr/share/nginx/html
    - $PWD/logs:/var/log/nginx
    - $PWD/conf.d:/etc/nginx/conf.d
    - $PWD/conf/nginx.conf:/etc/nginx/nginx.conf

然后访问 http://localhost:8080/wx/proxy/hello.html 就会跳转到 http://192.168.0.104:9999/hello.html

nginx 反向代理总结

location /a {            # 1 会跳转到 http://ip/a/xx
	proxy_pass http://ip;
}

location /a/ {           # 2 会跳转到 http://ip/xx 也就是会截掉 前缀 /a
	proxy_pass http://ip/;
}

location / {
	echo "/";
}

location /a {
	echo "/";
}

location ^~ /a {   # 以 /a 开头
	echo "/";
}

location =/a { # 优先级最高
	echo "/";
}


location /\w { # 通配符
	echo "/";
}


good luck !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值