nginx
[运行镜像]
: docker pull registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2
# mkdir /opt/data/nginx/:/usr/local/nginx/
: docker run \
--name nginx-blog \
-p 80:80 -p 443:443 \
-v /opt/data/nginx/:/usr/local/nginx/ \
-d -it registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2 \
./run.sh
[配置静态网站]
: ./nginx -s reload # 重启 nginx
: cd /opt/data/nginx/ && blog/ # 符合博客目录文件
: /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
// 添加服务配置
server {
listen 80;
server_name blog.thinxz.cn;
location / {
root blog;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
安装
[1、安装 基本编译工具及库]
: yum install update
: yum install -y \
vim wget curl
: yum install -y \
zlib zlib-devel libtool \
openssl openssl-devel \
gcc-c++ make
[2、安装 PCRE - 作用是让 Nginx 支持 Rewrite 功能]
: wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
: tar zxvf pcre-8.35.tar.gz && cd pcre-8.35
: ./configure && make //: make install # 不执行安装
: pcre-config --version
[3、安装 Nginx]
: wget http://nginx.org/download/nginx-1.6.2.tar.gz
// wget http://nginx.org/download/nginx-1.9.0.tar.gz
: tar zxvf nginx-1.6.2.tar.gz && cd nginx-1.6.2
// 简单编译项目
./configure --prefix=/opt/nginx/bin \
--with-pcre=/opt/nginx/pcre-8.35 \
--with-http_ssl_module \
--with-stream
# /opt/nginx/pcre-8.35 # 下载编译目录
# /usr/local/nginx # nginx 安装目录, 注意路径, 配置成安装路径
./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre=/opt/nginx/pcre-8.35
// --with-http_stub_status_module
--user=www --group=www \
--with-http_gzip_static_module \
--http-client-body-temp-path=/usr/local/nginx/tmp/client/ \
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ \
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ \
--with-poll_module \
--with-file-aio \
--with-http_realip_module \
--with-http_addition_module \
--with-http_addition_module \
--with-http_random_index_module --with-http_stub_status_module
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp
--http-scgi-temp-path=/usr/local/nginx/scgi_temp
: make // make install
: /usr/local/nginx/sbin/nginx -v
[commit]
: docker login --username=thinxz registry.cn-hangzhou.aliyuncs.com
: docker commit -m="ngrok" -a="thinxz" cid registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2
: docker push registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2
[run]
: docker run \
--name nginx09 \
-p 80:80 -p 443:443 \
-v /opt/data/nginx/:/usr/local/nginx/ \
-d -it registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:2 \
./run.sh
[run.sh]
#!/bin/sh
# run
rm -rf /usr/local/nginx/nginx-run.log
echo y | cp -rf /opt/nginx/ngconf/* /usr/local/nginx/
./opt/nginx/nginx-1.6.2/objs/nginx -c /usr/local/nginx/conf/nginx.conf 2>&1 | tee /usr/local/nginx/nginx-run.log
# suspend main
read -p "press any key to continue." var
配置
[1、创建Nginx 运行使用的用户]
: /usr/sbin/groupadd thinxz
: /usr/sbin/useradd -g thinxz thinxzpassword
: passwd thinxzpassword
[2、配置nginx.conf ]
: 查看配置文件 cat /usr/local/nginx/conf/nginx.conf
[3、测试配置文件]
: /usr/local/nginx/sbin/nginx -t
[4、启动Nginx]
: /usr/local/nginx/sbin/nginx
: /usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
: /usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx
: /usr/local/nginx/sbin/nginx -s stop # 停止 Nginx
[nginx docker run]
docker run \
--name nginx06 \
-p 80:80 -p 81:81 -p 443:443 -p 9876:9876 \
-v /opt/data/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/data/nginx/logs:/data/logs \
-d -it registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:v2
// -e 环境变量, 配置数据库
: docker run \
--name nginx06 \
-p 80:80 -p 81:81 -p 443:443 -p 9876:9876 \
-v /opt/data/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/data/nginx/logs:/data/logs \
-e DB_HOST=rm-bp10h4rjh8q877420lo.mysql.rds.aliyuncs.com \
-e DB_USER=nginx \
-e DB_PASS=nginx \
-e DB_PORT=3306 \
-d -it registry.cn-hangzhou.aliyuncs.com/thinxz/nginx:v2
//
docker run \
--name nginx \
-p 80:80 -p 443:443 -p 81:81 \
-e DB_HOST=rm-bp10h4rjh8q877420lo.mysql.rds.aliyuncs.com \
-e DB_USER=nginx \
-e DB_PASS=nginx \
-e DB_PORT=3306 \
-e DB=nginx \
-d registry.cn-hangzhou.aliyuncs.com/youdt/nginx:v2
// 宿主机端口只能映射一个容器端口, 一个容器端口可以被多个宿主机端口映射
// host:80 -> container:80
// host:80 -> container:80 && host:81 -> container:80
// 需求, nginx 容器监听宿主机80端口, 根据域名不同将80端口数据, 转发给不同业务容器
# 方案一
// host:80 -> container-ngxin:80
// nginx 映射配置 -> ngxin:80 -> (host:50001)
// host:50001 -> container-git:80
# 方案二
// host:80 -> container-ngxin:80
// nginx 映射配置 -> ngxin:80 -> container-git-ip:80
# 删除容器
// docker ps -a
// docker container rm container-id
# 删除镜像
// docker images
// docker image rm image-id
nginx-db.sql
nginx-s.sql
nginx.conf - 初始配置
# run nginx in foreground
daemon off;
user root;
# Set number of worker processes automatically based on number of CPU cores.
worker_processes auto;
# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;
error_log /data/logs/error.log warn;
# Includes files with directives to load dynamic modules.
include /etc/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
server_tokens off;
tcp_nopush on;
tcp_nodelay on;
client_body_temp_path /tmp/nginx/body 1 2;
keepalive_timeout 65;
ssl_prefer_server_ciphers on;
gzip on;
proxy_ignore_client_abort off;
client_max_body_size 2000m;
server_names_hash_bucket_size 64;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding "";
proxy_cache off;
proxy_cache_path /var/lib/nginx/cache/public levels=1:2 keys_zone=public-cache:30m max_size=192m;
proxy_cache_path /var/lib/nginx/cache/private levels=1:2 keys_zone=private-cache:5m max_size=1024m;
# MISS
# BYPASS
# EXPIRED - expired, request was passed to backend
# UPDATING - expired, stale response was used due to proxy/fastcgi_cache_use_stale updating
# STALE - expired, stale response was used due to proxy/fastcgi_cache_use_stale
# HIT
# - (dash) - request never reached to upstream module. Most likely it was processed at Nginx-level only (e.g. forbidden, redirects, etc) (Ref: Mail Thread
log_format proxy '[$time_local] $upstream_cache_status $upstream_status $status - $request_method $scheme $host "$request_uri" [Client $remote_addr] [Length $body_bytes_sent] [Gzip $gzip_ratio] [Sent-to $server] "$http_user_agent" "$http_referer"';
log_format standard '[$time_local] $status - $request_method $scheme $host "$request_uri" [Client $remote_addr] [Length $body_bytes_sent] [Gzip $gzip_ratio] "$http_user_agent" "$http_referer"';
access_log /data/logs/default.log proxy;
# Dynamically generated resolvers file
include /etc/nginx/conf.d/include/resolvers.conf;
# Default upstream scheme
map $host $forward_scheme {
default http;
}
# Real IP Determination
# Docker subnet:
set_real_ip_from 172.0.0.0/8;
# NPM generated CDN ip ranges:
include conf.d/include/ip_ranges.conf;
# always put the following 2 lines after ip subnets:
real_ip_header X-Forwarded-For;
real_ip_recursive on;
# Files generated by NPM
include /etc/nginx/conf.d/*.conf;
include /data/nginx/default_host/*.conf;
include /data/nginx/proxy_host/*.conf;
include /data/nginx/redirection_host/*.conf;
include /data/nginx/dead_host/*.conf;
include /data/nginx/temp/*.conf;
# config git.thinxz.cn:80 | host -> 172.17.0.1
server {
listen 443;
server_name git.thinxz.cn;
ssl on;
#ssl_certificate cert/2193315__sinoxx.com.pem;
#ssl_certificate_key cert/2193315__sinoxx.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_pass http://172.17.0.1:50001/;
}
}
server {
listen 80;
server_name git.thinxz.cn;
location / {
proxy_pass http://127.0.0.1:50002/;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
stream {
# 添加socket转发的代理
upstream socket_proxy {
hash $remote_addr consistent;
# 转发的目的地址和端口
server 192.168.1.100:9000 weight=5 max_fails=3 fail_timeout=30s;
}
# 提供转发的服务,即访问localhost:9001, 会跳转至代理socket_proxy指定的转发地址
server {
listen 443;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass socket_proxy;
}
}
stream {
# Files generated by NPM
include /data/nginx/stream/*.conf;
}
//
upstream git_pool{
server 127.0.0.1:8081;
}
server {
listen 80; #拦截端口
server_name git.thinxz.cm; #域名配置
access_log logs/git.log;
error_log logs/git.error;
# 将所有请求转发给git_pool池的应用处理
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://git_pool; #如果是ssl更改成https
}
}
upstream git_pool{
server 127.0.0.1:8081;
}
server {
listen 80; #拦截端口
server_name git.thinxz.cm; #域名配置
access_log logs/git.log;
error_log logs/git.error;
# 将所有请求转发给git_pool池的应用处理
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://git_pool; #如果是ssl更改成https
}
}
nginx.conf
[nginx.conf]
user thinxzNginx thinxz123456;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
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';
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server
{
listen 80;#监听端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
}