1、nginx 下载: 谷歌直接搜 nginx 下载
2、选择版本。这里选择的是1.18.0版本
yum install -y wget
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0
3、第三方模块地址:可以通过安装git来拉取数据,
git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
需要先安装git :
yum -y install git
也可以通过下载它对应的zip包, 执行wget 拉取下来再解压。
4、默认的nginx 1.18版本的configure(这里是安装完后执行nginx -V看到的):
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.18.0/debian/debuild-base/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
这个参数我去掉了–user=nginx --group=nginx ,不需要指定用户和用户组,如果指定了,在启动nginx时 不存在nginx:nginx 还会报错
5、开始安装第三方模块
cd nginx-1.18.0
安装patch
yum -y install patch
patch -p1 < /usr/local/connect_module/ngx_http_proxy_connect_module/patch/选择对应patch
(github ngx_http_proxy_connect_module的readme 中有写https://github.com/chobits/ngx_http_proxy_connect_module#example-for-curl)
安装时的一些依赖也要安装下
yum -y install gcc-c++ && yum -y install pcre-devel openssl openssl-devel
在上面的配置的最后加上 –add-module=/usr/local/connect_module/
完整的配置:
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.18.0/debian/debuild-base/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=/具体路径/ngx_http_proxy_connect_module
6、执行make && make install (目前是初次安装nginx时 带上第三方模块)
7、nginx -v 验证一下
8、Nginx -V 可以看nginx的版本 还有配置,查看加的模块
9、nginx开启nginx服务,修改nginx.conf后续重启nginx
nginx 命令
nginx 启动nginx
nginx -V #显示版本和配置选项信息, 可以看到编译时的配置信息
nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
nginx -t #检测配置文件是否有语法错误,
nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx
Dockerfile:
FROM centos:7.5.1804
WORKDIR /usr/nginx
# instal utils software
RUN yum -y install gcc-c++ && yum -y install pcre-devel openssl openssl-devel \
&& yum -y install patch unzip
# install nginx
RUN curl -O http://nginx.org/download/nginx-1.18.0.tar.gz \
&& tar -zxf nginx-1.18.0.tar.gz \
&& rm -f nginx-1.18.0.tar.gz \
&& curl -L -O https://github.com/chobits/ngx_http_proxy_connect_module/archive/refs/tags/v0.0.2.zip \
&& unzip v0.0.2.zip \
&& rm -f v0.0.2.zip \
&& mv ngx_http_proxy_connect_module-0.0.2 ngx_http_proxy_connect_module \
&& cd nginx-1.18.0 \
&& patch -p1 < /usr/nginx/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch \
&& ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-compat --with-file-aio --with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module --with-http_flv_module \
--with-http_gunzip_module --with-http_gzip_static_module \
--with-http_mp4_module --with-http_random_index_module \
--with-http_realip_module --with-http_secure_link_module \
--with-http_slice_module --with-http_ssl_module \
--with-http_stub_status_module --with-http_sub_module \
--with-http_v2_module --with-mail --with-mail_ssl_module \
--with-stream --with-stream_realip_module --with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.18.0/debian/debuild-base/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' \
--add-module=/usr/nginx/ngx_http_proxy_connect_module \
&& make && make install && mkdir -p /var/cache/nginx \
&& mkdir -p /etc/nginx/logs
COPY nginx.conf /etc/nginx/nginx.conf
# use -v to replace /etc/nginx/nginx.conf with local nginx.conf when running
# container
CMD ["nginx", "-g", "daemon off;"]
生成镜像文件:
docker build .
nginx.conf
#user nobody;
worker_processes 1;
#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;
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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8502; # nginx服务端口号
server_name localhost;
resolver 114.114.114.114;
proxy_connect;
proxy_connect_allow all;
proxy_connect_connect_timeout 60s;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_set_header Host $http_host;
proxy_connect_timeout 70s;
proxy_http_version 1.1;
proxy_pass $scheme://$http_host$request_uri;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
启动命令:
docker run -d --name nginx-test -v /local/path/nginx.conf:/etc/nginx/nginx.conf --network=host imageId