- 浏览: 2552833 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Build Nginx Binary in Docker and Using If Directive
When we should use break in If
http://agentzh.blogspot.com/2011/03/how-nginx-location-if-works.html
If is evil
https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/
Official document about If
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if
Instead of using Map, I use if to check the header in request
server {
listen 8443;
client_max_body_size 10M;
# redirect pairing and status check traffic to aws lambda
resolver 8.8.4.4 8.8.8.8;
set $ocpServer 'https://ocp.{{targetDomain}}';
location / {
if ( $http_user_agent = 'oldclientsproxy' ) {
proxy_pass http://local-external-ip:5080;
}
if ( $http_user_agent != 'oldclientsproxy' ) {
proxy_pass $ocpServer;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
}
ssl on;
ssl_certificate /usr/local/nginx-1.14.0/ssl/cert.pem;
ssl_certificate_key /usr/local/nginx-1.14.0/ssl/cert.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ‘xxxxxxxxxxMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_dhparam /usr/local/nginx-1.14.0/ssl/dhparams.pem;
ssl_prefer_server_ciphers on;
}
So the request will go to system DNS and go through /etc/host for oldclientsproxy request and go to local-external-ip:5080.
I use $ocpServer which is a variable there, so nginx will go to resolver 8.8.8.8 to look up the DNS.
Here is how I build the nginx binary from the source on top of Ubuntu 12.04
Makefile
IMAGE=sillycat/sillycat-localproxy
TAG=1.0
NAME=sillycat-localproxy
REPOSITORY=xxxxxxx.dkr.ecr.us-west-1.amazonaws.com
push-local:
docker push $(REPOSITORY)/$(IMAGE):$(TAG)
docker-context:
prepare:
rm -fr ./install
rm -fr ./dist
mkdir ./install
wget https://nginx.org/download/nginx-1.14.0.tar.gz -P ./install/
mkdir ./dist
build: docker-context
docker build -t $(REPOSITORY)/$(IMAGE):$(TAG) .
run-dev:
docker run -e RUNNING_ENV=dev -v ${CURDIR}/dist:/dist --name $(NAME) $(REPOSITORY)/$(IMAGE):$(TAG)
run-stage:
docker run -e RUNNING_ENV=stage -v ${CURDIR}/dist:/dist --name $(NAME) $(REPOSITORY)/$(IMAGE):$(TAG)
debug:
docker run -ti -e RUNNING_ENV=dev -v ${CURDIR}/dist:/dist --name $(NAME) $(REPOSITORY)/$(IMAGE):$(TAG) /bin/bash
clean:
docker stop ${NAME}
docker rm ${NAME}
logs:
docker logs -t -f ${NAME}
publish:
docker push ${IMAGE}
Dockerfile
FROM ubuntu:12.04
#prepare OS
RUN apt-get -y update
RUN apt-get install -y procps
RUN apt-get install -y vim-tiny
RUN apt-get install -y sudo
RUN apt-get install -y python-pip python-dev build-essential
RUN apt-get install -y libpcre3 libpcre3-dev
#prepare the resources
RUN mkdir -p /install/
RUN mkdir -p /dist/
RUN mkdir -p /tool/conf
RUN chmod -R a+x /tool/conf
RUN mkdir -p /tool/ssl/
ADD ./install/nginx-1.14.0.tar.gz /install/
ADD template/nginx.conf /tool/template/
ADD template/ngproxy /tool/template/
ADD script/conf_gen.py /tool/script/
ADD conf/cert-*.pem /tool/ssl/
ADD conf/cert-*.key /tool/ssl/
ADD conf/dhparams-*.pem /tool/ssl/
#compile nginx
#set up the python engine env
RUN apt-get install -y python-jinja2
WORKDIR /tool/
RUN mkdir -p /app/
ADD start.sh /app/
CMD /app/start.sh
Start.sh
#!/bin/sh -ex
#prepare the configuration
python script/conf_gen.py
#compile the nginx
cd /install/nginx-1.14.0
./configure --prefix=/usr/local/nginx-1.14.0 --with-http_ssl_module
make
make install
#overwrite the configuration
mkdir -p /usr/local/nginx-1.14.0/sites-available
mkdir -p /usr/local/nginx-1.14.0/sites-enabled
mkdir -p /usr/local/nginx-1.14.0/ssl
cp /tool/conf/nginx.conf /usr/local/nginx-1.14.0/conf/nginx.conf
cp /tool/conf/ngproxy /usr/local/nginx-1.14.0/sites-available/ngproxy
ln -s /usr/local/nginx-1.14.0/sites-available/ngproxy /usr/local/nginx-1.14.0/sites-enabled/ngproxy
cp /tool/ssl/cert-${RUNNING_ENV}.key /usr/local/nginx-1.14.0/ssl/cert.key
cp /tool/ssl/cert-${RUNNING_ENV}.pem /usr/local/nginx-1.14.0/ssl/cert.pem
cp /tool/ssl/dhparams-${RUNNING_ENV}.pem /usr/local/nginx-1.14.0/ssl/dhparams.pem
#compress to dist
cd /usr/local/
tar czf nginx-1.14.0-bin.tar.gz nginx-1.14.0
cp /usr/local/nginx-1.14.0-bin.tar.gz /dist/nginx-1.14.0-bin.tar.gz
Using Python Script script/conf_gen.py to generate the configuration from template
#!/usr/bin/python
from jinja2 import Environment, FileSystemLoader
import os
from sys import exit
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
ROOT_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '../'))
TEMPLATE_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'template'))
CONF_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'conf'))
print("SCRIPT = " + SCRIPT_DIR)
print("TEMPLATE = " + TEMPLATE_DIR)
print("CONF = " + CONF_DIR)
env_domain_mappings = {
'dev': ‘sillycatclouddev.com',
'stage': ‘sillycatcloudbeta.com',
'prod': ‘sillycatcloud.com'
}
def generateConf():
try:
runningEnv = os.environ['RUNNING_ENV']
except KeyError as e:
exit("RUNNING_ENV is not set in ENV, exit!")
print("Generating nginx.conf for " + runningEnv)
generate_conf_env = {
'runningEnv': runningEnv
}
print(generate_conf_env)
generateFile('template/nginx.conf', 'conf/nginx.conf', generate_conf_env)
def generateProxy():
try:
runningEnv = os.environ['RUNNING_ENV']
except KeyError as e:
exit("{0} is not set in environment".format(e))
print("Generating ngproxy for " + runningEnv)
targetDomain = env_domain_mappings[runningEnv]
generate_proxy_env = {
'targetDomain':targetDomain
}
print(generate_proxy_env)
generateFile('template/ngproxy', 'conf/ngproxy', generate_proxy_env)
def generateFile(template_name, output_file_name, params):
env = Environment(loader=FileSystemLoader(ROOT_DIR), trim_blocks=True)
output_content = env.get_template(template_name).render(params)
with open(output_file_name, 'w') as file:
file.write(output_content)
if __name__ == '__main__':
print("Generate the nginx.conf file")
generateConf()
print("Generate the ngproxy file")
generateProxy()
It is working great.
References:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#break
http://agentzh.blogspot.com/2011/03/how-nginx-location-if-works.html
https://stackoverflow.com/questions/32825703/syntax-for-if-statement-in-nginx
When we should use break in If
http://agentzh.blogspot.com/2011/03/how-nginx-location-if-works.html
If is evil
https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/
Official document about If
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if
Instead of using Map, I use if to check the header in request
server {
listen 8443;
client_max_body_size 10M;
# redirect pairing and status check traffic to aws lambda
resolver 8.8.4.4 8.8.8.8;
set $ocpServer 'https://ocp.{{targetDomain}}';
location / {
if ( $http_user_agent = 'oldclientsproxy' ) {
proxy_pass http://local-external-ip:5080;
}
if ( $http_user_agent != 'oldclientsproxy' ) {
proxy_pass $ocpServer;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
}
ssl on;
ssl_certificate /usr/local/nginx-1.14.0/ssl/cert.pem;
ssl_certificate_key /usr/local/nginx-1.14.0/ssl/cert.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ‘xxxxxxxxxxMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_dhparam /usr/local/nginx-1.14.0/ssl/dhparams.pem;
ssl_prefer_server_ciphers on;
}
So the request will go to system DNS and go through /etc/host for oldclientsproxy request and go to local-external-ip:5080.
I use $ocpServer which is a variable there, so nginx will go to resolver 8.8.8.8 to look up the DNS.
Here is how I build the nginx binary from the source on top of Ubuntu 12.04
Makefile
IMAGE=sillycat/sillycat-localproxy
TAG=1.0
NAME=sillycat-localproxy
REPOSITORY=xxxxxxx.dkr.ecr.us-west-1.amazonaws.com
push-local:
docker push $(REPOSITORY)/$(IMAGE):$(TAG)
docker-context:
prepare:
rm -fr ./install
rm -fr ./dist
mkdir ./install
wget https://nginx.org/download/nginx-1.14.0.tar.gz -P ./install/
mkdir ./dist
build: docker-context
docker build -t $(REPOSITORY)/$(IMAGE):$(TAG) .
run-dev:
docker run -e RUNNING_ENV=dev -v ${CURDIR}/dist:/dist --name $(NAME) $(REPOSITORY)/$(IMAGE):$(TAG)
run-stage:
docker run -e RUNNING_ENV=stage -v ${CURDIR}/dist:/dist --name $(NAME) $(REPOSITORY)/$(IMAGE):$(TAG)
debug:
docker run -ti -e RUNNING_ENV=dev -v ${CURDIR}/dist:/dist --name $(NAME) $(REPOSITORY)/$(IMAGE):$(TAG) /bin/bash
clean:
docker stop ${NAME}
docker rm ${NAME}
logs:
docker logs -t -f ${NAME}
publish:
docker push ${IMAGE}
Dockerfile
FROM ubuntu:12.04
#prepare OS
RUN apt-get -y update
RUN apt-get install -y procps
RUN apt-get install -y vim-tiny
RUN apt-get install -y sudo
RUN apt-get install -y python-pip python-dev build-essential
RUN apt-get install -y libpcre3 libpcre3-dev
#prepare the resources
RUN mkdir -p /install/
RUN mkdir -p /dist/
RUN mkdir -p /tool/conf
RUN chmod -R a+x /tool/conf
RUN mkdir -p /tool/ssl/
ADD ./install/nginx-1.14.0.tar.gz /install/
ADD template/nginx.conf /tool/template/
ADD template/ngproxy /tool/template/
ADD script/conf_gen.py /tool/script/
ADD conf/cert-*.pem /tool/ssl/
ADD conf/cert-*.key /tool/ssl/
ADD conf/dhparams-*.pem /tool/ssl/
#compile nginx
#set up the python engine env
RUN apt-get install -y python-jinja2
WORKDIR /tool/
RUN mkdir -p /app/
ADD start.sh /app/
CMD /app/start.sh
Start.sh
#!/bin/sh -ex
#prepare the configuration
python script/conf_gen.py
#compile the nginx
cd /install/nginx-1.14.0
./configure --prefix=/usr/local/nginx-1.14.0 --with-http_ssl_module
make
make install
#overwrite the configuration
mkdir -p /usr/local/nginx-1.14.0/sites-available
mkdir -p /usr/local/nginx-1.14.0/sites-enabled
mkdir -p /usr/local/nginx-1.14.0/ssl
cp /tool/conf/nginx.conf /usr/local/nginx-1.14.0/conf/nginx.conf
cp /tool/conf/ngproxy /usr/local/nginx-1.14.0/sites-available/ngproxy
ln -s /usr/local/nginx-1.14.0/sites-available/ngproxy /usr/local/nginx-1.14.0/sites-enabled/ngproxy
cp /tool/ssl/cert-${RUNNING_ENV}.key /usr/local/nginx-1.14.0/ssl/cert.key
cp /tool/ssl/cert-${RUNNING_ENV}.pem /usr/local/nginx-1.14.0/ssl/cert.pem
cp /tool/ssl/dhparams-${RUNNING_ENV}.pem /usr/local/nginx-1.14.0/ssl/dhparams.pem
#compress to dist
cd /usr/local/
tar czf nginx-1.14.0-bin.tar.gz nginx-1.14.0
cp /usr/local/nginx-1.14.0-bin.tar.gz /dist/nginx-1.14.0-bin.tar.gz
Using Python Script script/conf_gen.py to generate the configuration from template
#!/usr/bin/python
from jinja2 import Environment, FileSystemLoader
import os
from sys import exit
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
ROOT_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '../'))
TEMPLATE_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'template'))
CONF_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'conf'))
print("SCRIPT = " + SCRIPT_DIR)
print("TEMPLATE = " + TEMPLATE_DIR)
print("CONF = " + CONF_DIR)
env_domain_mappings = {
'dev': ‘sillycatclouddev.com',
'stage': ‘sillycatcloudbeta.com',
'prod': ‘sillycatcloud.com'
}
def generateConf():
try:
runningEnv = os.environ['RUNNING_ENV']
except KeyError as e:
exit("RUNNING_ENV is not set in ENV, exit!")
print("Generating nginx.conf for " + runningEnv)
generate_conf_env = {
'runningEnv': runningEnv
}
print(generate_conf_env)
generateFile('template/nginx.conf', 'conf/nginx.conf', generate_conf_env)
def generateProxy():
try:
runningEnv = os.environ['RUNNING_ENV']
except KeyError as e:
exit("{0} is not set in environment".format(e))
print("Generating ngproxy for " + runningEnv)
targetDomain = env_domain_mappings[runningEnv]
generate_proxy_env = {
'targetDomain':targetDomain
}
print(generate_proxy_env)
generateFile('template/ngproxy', 'conf/ngproxy', generate_proxy_env)
def generateFile(template_name, output_file_name, params):
env = Environment(loader=FileSystemLoader(ROOT_DIR), trim_blocks=True)
output_content = env.get_template(template_name).render(params)
with open(output_file_name, 'w') as file:
file.write(output_content)
if __name__ == '__main__':
print("Generate the nginx.conf file")
generateConf()
print("Generate the ngproxy file")
generateProxy()
It is working great.
References:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#break
http://agentzh.blogspot.com/2011/03/how-nginx-location-if-works.html
https://stackoverflow.com/questions/32825703/syntax-for-if-statement-in-nginx
发表评论
-
Stop Update Here
2020-04-28 09:00 316I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 369Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 370Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 431Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 456VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 479NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 424Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 452GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ...
相关推荐
nginx arm版本docker镜像文件
无标题nginx1.26.2的docker镜像文件
资源包含Dockerfile文件,可以直接构建自己的nginx镜像
arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构...
nginx1.26.2版本docker镜像
centos7离线部署nginx-docker,需要的可以下载免费公开,记录自己的一次学习 centos7离线部署nginx-docker,需要的可以下载免费公开,记录自己的一次学习
arm下nginx docker镜像
nginx+lua+docker+docker-compose实现简单服务分发
镜像包最新的
上传nginx docker镜像,供那些不能从官方仓库中下载的同学们,下载离线使用,体验容器技术的魅力。
docker-nginx, Nginx Docker 映像,可以轻松承载 static 站点 dockerDocker的高性能 Nginx 基础映像,用于 static 网站。 它将服务于 /var/www 目录中的任何内容。要为你的站点构建 Docker 映像,你需要创建一个 ...
nginx.tar-Nginx镜像,导入docker直接使用
docker - nginx镜像
该资源包括Linux系统nginx安装包,docker安装包,jdk的安装包以及docker容器的nginx启动脚本,可用于前端jar包部署,只需把前端包放在nginx/www/html目录下,启动nginx脚本即可,拥有搭建redis+sentinel集群的文件以及...
docker pull nginx ``` 二、启动 MySQL 容器 我们需要启动 MySQL 容器,以便 WordPress 使用。我们可以使用以下命令启动 MySQL 容器: ``` docker run -d \ --name wordpressdb \ -p 3306:3306 \ --env MYSQL_...
docker离线安装nginx镜像
使用Docker技术安装Nginx及配置简单的负载均衡。 将nginx的配置文件从容器中拷贝到宿主目录 $ mkdir -p /colorfulfrog/nginx/config --先在宿主机创建config目录 $ mkdir -p /colorfulfrog/nginx/html --先在宿主机...
本文将详细介绍如何在Docker环境下配置Nginx以实现这一功能,主要涉及`docker-compose.yml`、`nginx.conf`以及`mime.types`文件的配置。 首先,我们来看`docker-compose.yml`文件。这个文件用于定义和配置Docker...
nginx容器内安装目录: /home/nginx/nginx_stream 免重新编译,解决1.24.0 编译sticky 失败问题,启动即用
官方nginx 镜像不带主动健康,本镜像将 nginx_upstream_check健康检查 打包到了镜像中。