×

Nginx正向代理http请求

hqy hqy 发表于2019-03-17 03:39:15 浏览2108 评论0

抢沙发发表评论

1.简单的正向代理

#正向代理http

 server {

   #     access_log /home/bingchenglin/logs/nginx/access.log;   日志一般最上面配置全局         

        listen 8082;

        location / { 


               resolver 8.8.8.8;#DNS解析 可以用这个

                proxy_pass $scheme://$http_host$request_uri;

                proxy_buffers   256 4k;

                 proxy_max_temp_file_size 0k;

       }

}

该访问会默认访问80端口 有些网址不是80端口或者图片不能加载


2.通过加入Lua模块对非80端口进行访问

这里有个地址:https://www.cnblogs.com/aoeiuv/p/6856056.html


找到的一个配置文件 


if ($http_host ~ "(:(\d){4})$"){


   set_by_lua $port "    


   local host = ngx.req.get_essay-headers()[\"host\"];    


   local port = string.sub(host,-4);    


   return port ";    


set $passport $port ;  


 proxy_pass http://$host:$passport$request_uri;


}


if ($http_host ~ "(:(\d){3})$"){  


 set_by_lua $port "    


   local host = ngx.req.get_essay-headers()[\"host\"];      


 local port = string.sub(host,-3);    


   return port ";  


 set $passport $port ;  


 proxy_pass http://$host:$passport$request_uri; }


if ($http_host ~ "(:(\d){2})$"){  


 set_by_lua $port "    


   local host = ngx.req.get_essay-headers()[\"host\"];  


     local port = string.sub(host,-2);    


   return port ";  


 set $passport $port ;  


 proxy_pass http://$host:$passport$request_uri;


} if ($http_host !~ :){      


 proxy_pass http://$host$request_uri;


}


 



Nginx安装lua支持

需要LuaJIT-2.0.4.tar.gzngx_devel_kitlua-nginx-module

1.下载安装LuaJIT-2.0.4.tar.gz

wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz

tar xzvf LuaJIT-2.0.4.tar.gz

cd LuaJIT-2.0.4

make install PREFIX=/usr/local/luajit

 

#注意环境变量!

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

2.下载解压ngx_devel_kit

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

tar -xzvf v0.3.0.tar.gz

3.下载解压lua-nginx-module

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz

tar -xzvf v0.10.8.tar.gz

 

4.下载安装nginx-1.10.3.tar.gz

wget http://nginx.org/download/nginx-1.10.3.tar.gz

tar -xzvf nginx-1.10.3.tar.gz

cd nginx-1.10.3

./configure --add-module=/opt/soft/ngx_devel_kit-0.3.0 --add-module=/opt/soft/lua-nginx-module-0.10.8

#注意ngx_devel_kitlua-nginx-module以实际解压路径为准

make -j2

make install

 

注:报错gcc需要安装,可以执行

yum install -y gcc g++ gcc-c++

依赖报错,可以执行

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

 

5.验证

#nginx做成命令

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

cd /usr/local/nginx/conf/

vi nginx.conf

 

#lua指令方式

#server 中添加一个localtion

location /hello {

            default_type 'text/plain';

            content_by_lua 'ngx.say("hello, lua")';

        }

#lua文件方式

#server 中添加一个localtion

location /lua {

    default_type 'text/html';

    content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录

}

#test.lua文件内容

ngx.say("hello world");

 

#启动nginx(已经做了软连接了,可以在任何目录启动)

nginx

 

注:

报错nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 可以执行

ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

具体source的路径已实际安装路径为准

 

 

访问:

http://127.0.0.1/hello

显示:hello, lua

http://127.0.0.1/lua

显示:hello world

 

到这里确定安装成功。


打赏

本文链接:https://www.kinber.cn/post/374.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客