因为业务系统需求,需要对web服务作nginx代理,在不断的尝试过程中,简单总结了一下常见的nginx代理配置。
下载安装
下载源码
wget http://nginx.org/download/nginx-1.13.4.tar.gz
这是1.2.8版本其他版本自己下载。
安装
#解压
tar -zxvf nginx-1.2.8.tar.gz
#进入目录
cd nginx-1.2.8
配置安转目录
./configure --prefix=/usr/local/nginx --with-stream
编译
make
make install
——————————————-分界线开始—————————————-
最近配置的时候报错缺少依赖如下:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解决:
$ apt-get update
$ apt-get install libpcre3 libpcre3-dev
# wget http://sourceforge.net/projects/pcre/files/pcre/8.32/pcre-8.32.tar.gz/download
# tar -xzvf pcre-8.32.tar.gz
# cd pcre-8.32
# ./configure --prefix=/usr/local/pcre
# make && make install
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决:
$ sudo apt-get install zlib1g-dev
最暴力的解决(可能运行会报错):
$ ./configure --prefix=/usr/local/nginx --without-http_gzip_module
——————————————-分界线结束—————————————-
至此安装完成
验证
启动:
/usr/local/nginx/sbin/nginx
打开浏览器:
输入安装nginx的ip
出现:
Welcome to nginx!
转发配置
vi /usr/local/nginx/conf/nginx.conf
注意两个关键词:location 和proxy_pass
location:就是后缀,proxy_pass:就是转发到的相应的ip及其端口。
如下:
location /{
proxy_pass http://localhost:8080/index.html
}
#这句话就是说把所有的localhost请求转发到http://localhost:8080/index.html
location /test{
proxy_pass http://localhost:8080/test/index.html
}
#这句话就是说把所有的localhost/test请求转发到http://localhost:8080/test/index.html
#注意:不要location 不要写成 /test/ 因为location的pattern识别的路径作为绝对路径。
是不是很简单,对很简单,不过这是nginx最基础的用法,if you want to more , 进入官网吧
———分界线——-
如果需要使用https相关服务,–with-http_ssl_module
sudo apt-get install openssl libssl-dev