天天看點

docker-componse nginx 反向代理

docker-componse nginx 反向代理

工程目錄

[email protected]:/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 !

繼續閱讀