Skip to content

通过环境变量传递参数

在使用docker compose运行openresty时,通过使用环境变量传递参数。

详细用法请参考 链接

  • .env定义变量

    properties
    proxyTargetProtocol=https
    proxyTargetHost=163.com
    proxyTargetPort=443
  • docker-compose.yaml通过环境变量传递参数到容器实例中

    yaml
    version: "3.0"
    
    services:
      openresty:
        image: registry.cn-hangzhou.aliyuncs.com/future-public/demo-openresty-base-dev
        ports:
          - 80:80
        environment:
          - proxyTargetProtocolEnv=${proxyTargetProtocol}
          - proxyTargetHostEnv=${proxyTargetHost}
          - proxyTargetPortEnv=${proxyTargetPort}
          - TZ=Asia/Shanghai
        volumes:
          - ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf
  • 设置环境变量到nginx变量中

    nginx
    # nginx.conf  --  docker-openresty
    #
    # This file is installed to:
    #   `/usr/local/openresty/nginx/conf/nginx.conf`
    # and is the file loaded by nginx at startup,
    # unless the user specifies otherwise.
    #
    # It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
    # section and adds this directive:
    #     `include /etc/nginx/conf.d/*.conf;`
    #
    # The `docker-openresty` file `nginx.vh.default.conf` is copied to
    # `/etc/nginx/conf.d/default.conf`.  It contains the `server section
    # of the upstream `nginx.conf`.
    #
    # See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
    #
    
    #user  nobody;
    #worker_processes 1;
    
    # Enables the use of JIT for regular expressions to speed-up their processing.
    pcre_jit on;
    
    
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        # Enables or disables the use of underscores in client request header fields.
        # When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.
        # underscores_in_headers off;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
            # Log in JSON Format
            # log_format nginxlog_json escape=json '{ "timestamp": "$time_iso8601", '
            # '"remote_addr": "$remote_addr", '
            #  '"body_bytes_sent": $body_bytes_sent, '
            #  '"request_time": $request_time, '
            #  '"response_status": $status, '
            #  '"request": "$request", '
            #  '"request_method": "$request_method", '
            #  '"host": "$host",'
            #  '"upstream_addr": "$upstream_addr",'
            #  '"http_x_forwarded_for": "$http_x_forwarded_for",'
            #  '"http_referrer": "$http_referer", '
            #  '"http_user_agent": "$http_user_agent", '
            #  '"http_version": "$server_protocol", '
            #  '"nginx_access": true }';
            # access_log /dev/stdout nginxlog_json;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
            #access_log  /var/log/nginx/host.access.log  main;
    
            # 设置环境变量到nginx变量中
            set_by_lua $proxyTargetProtocolInternal 'return os.getenv("proxyTargetProtocolEnv")';
            set_by_lua $proxyTargetHostInternal 'return os.getenv("proxyTargetHostEnv")';
            set_by_lua $proxyTargetPortInternal 'return os.getenv("proxyTargetPortEnv")';
    
            # nginx配置引用变量
            location / {
                resolver 114.114.114.114;
                proxy_pass $proxyTargetProtocolInternal://$proxyTargetHostInternal:$proxyTargetPortInternal;
            }
    
            # lua脚本引用变量
            location /lua {
                content_by_lua_block {
                    ngx.header.content_type = "text/plain;charset=utf-8";
                    local response = "proxy target host: " .. ngx.var.proxyTargetHostInternal;
                    ngx.say(response);
                }
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   /usr/local/openresty/nginx/html;
            }
        }
    
        # Don't reveal OpenResty version to clients.
        # server_tokens off;
    }
    
    # 声明环境变量
    env proxyTargetProtocolEnv;
    env proxyTargetHostEnv;
    env proxyTargetPortEnv;