正向代理访问非80端口的服务

    1. FROM centos:7
    2. ENV TZ=Asia/Shanghai
    3. RUN cd /etc/yum.repos.d \
    4. && curl -O http://mirrors.aliyun.com/repo/Centos-7.repo \
    5. && yum makecache \
    6. && yum -y install gcc g++ make tar openssl openssl-devel pcre-devel pcre rpm-build rpmdevtools lua lua-devel
    7. # https://openresty.org/download/openresty-1.15.8.3.tar.gz
    8. COPY openresty-1.15.8.3.tar.gz /opt
    9. # https://github.com/chobits/ngx_http_proxy_connect_module
    10. COPY ngx_http_proxy_connect_module.tgz /opt
    11. RUN cd /opt && tar xaf openresty-1.15.8.3.tar.gz && tar xaf ngx_http_proxy_connect_module.tgz \
    12. && cd /opt/openresty-1.15.8.3 \
    13. && ./configure --prefix=/etc/openresty --with-http_stub_status_module --with-http_sub_module --with-http_auth_request_module --with-http_addition_module --add-module=/opt/ngx_http_proxy_connect_module \
    14. && patch -d build/nginx-1.15.8/ -p 1 < /opt/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101504.patch \
    15. && make -j "$(nproc)" && make install \
    16. && mkdir -p /etc/openresty/nginx/conf/conf.d \
    17. && ln -s /etc/openresty/nginx/sbin/nginx /usr/sbin/
    18. EXPOSE 80
    19. # vi nginx.conf: include /etc/openresty/nginx/conf/conf.d/*.conf
    20. VOLUME /etc/openresty/nginx/conf/conf.d
    21. CMD ["nginx", "-g", "daemon off;"]

    /etc/openresty/nginx/conf/conf.d/acdiost.conf

    1. server {
    2. listen 80;
    3. # dns resolver used by forward proxying
    4. resolver 8.8.8.8;
    5. # forward proxy for CONNECT request
    6. proxy_connect;
    7. proxy_connect_allow all;
    8. proxy_connect_connect_timeout 10s;
    9. proxy_connect_read_timeout 10s;
    10. proxy_connect_send_timeout 10s;
    11. # forward proxy for non-CONNECT request
    12. location / {
    13. if ($http_host ~ "(:(\d){5})$"){
    14. set_by_lua $port "
    15. local host = ngx.req.get_headers()[\"host\"];
    16. local port = string.sub(host,-5);
    17. return port ";
    18. set $passport $port ;
    19. proxy_pass http://$host:$passport$request_uri;
    20. }
    21. if ($http_host ~ "(:(\d){4})$"){
    22. set_by_lua $port "
    23. local host = ngx.req.get_headers()[\"host\"];
    24. local port = string.sub(host,-4);
    25. return port ";
    26. set $passport $port ;
    27. proxy_pass http://$host:$passport$request_uri;
    28. }
    29. if ($http_host ~ "(:(\d){3})$"){
    30. set_by_lua $port "
    31. local host = ngx.req.get_headers()[\"host\"];
    32. local port = string.sub(host,-3);
    33. return port ";
    34. set $passport $port ;
    35. proxy_pass http://$host:$passport$request_uri;
    36. }
    37. if ($http_host ~ "(:(\d){2})$"){
    38. set_by_lua $port "
    39. local host = ngx.req.get_headers()[\"host\"];
    40. local port = string.sub(host,-2);
    41. return port ";
    42. set $passport $port ;
    43. proxy_pass http://$host:$passport$request_uri;
    44. }
    45. if ($http_host !~ :){
    46. proxy_pass http://$host$request_uri;
    47. }
    48. proxy_pass http://$host;
    49. proxy_set_header Host $host;
    50. client_max_body_size 2000m;
    51. proxy_set_header terminal_id $http_terminal_id;
    52. proxy_set_header Host $host:$server_port;
    53. proxy_set_header X_Read_IP $remote_addr;
    54. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    55. proxy_set_header X-Forwarded-Host $host;
    56. proxy_set_header X-Forwarded-Server $host;
    57. proxy_set_header X-Forwarded-Proto $scheme;
    58. }
    59. # 测试lua
    60. location /lua {
    61. default_type 'text/html';
    62. content_by_lua 'ngx.say("hello lua")';
    63. }
    64. }

    测试结果
    image.png
    浏览器配置代理即可。谷歌浏览器需安装插件 Proxy SwitchyOmega