安装最新版
docker pull nginx
docker images
主机服务器创建目录,新建nginx.conf配置,如:
/user/local/nginx/nginx.conf
使用-v挂载主机的nginx.conf,修改比较方便:
docker run --name nginx -p 80:80 -d nginx -v /user/local/nginx/nginx.conf:/etc/nginx/nginx.conf
查看是否行成功
docker ps
如果/user/local/nginx/nginx.conf配置修改了,那么docker中正在运行的nginx容器需要reload:
docker restart 08c28d2d7bfe
示例配置文件:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
upstream myserver {
server 192.168.7.222:8081 weight=10;
server 192.168.7.222:8082 weight=5;
}
server {
listen 80;
server_name 192.168.7.222;
location / {
root html;
proxy_pass http://myserver;
index index.html index.htm;
}
location /a {
root html;
proxy_pass http://192.168.7.222:8081;
index index.html index.htm;
}
location /b {
root html;
proxy_pass http://192.168.7.222:8082;
index index.html index.htm;
}
}
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
如果配置有问题,启动报错,赶紧使用命令查看一下日志:
docker logs 容器ID
一眼就看出哪里报错了吧。
配置文件中使用正则的最佳姿势:
upstream myserver {
server 192.168.7.222:8081 weight=10;
server 192.168.7.222:8082 weight=5;
}
server {
listen 80;
server_name ~^(?<subdomain>.+)\.donge\.com$;
#使用正则一定要波浪符 ~
#变量名定义格式:(?<变量名>.+)
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
set $port 8081;
#if和(之间必须空格,否则if不能识别
#$subdomain和=之间必须要有空格,否则$subdomain='bb'整个当作变量就报错了
if ($subdomain = 'bb' ){
set $port 8082;
}
location / {
root html;
#负载均衡使用upstream定义的服务器名
#proxy_pass http://myserver;
#使用变量$port
proxy_pass http://192.168.7.222:$port;
index index.html index.htm;
}
location /a {
root html;
proxy_pass http://192.168.7.222:8081;
index index.html index.htm;
}
location /b {
root html;
proxy_pass http://192.168.7.222:8082;
index index.html index.htm;
}
}
姿势总结:
- 使用正则一定要波浪符 ~
- 变量名定义格式:(?<变量名>.+)
- if和(之间必须空格,否则if不能识别
- s u b d o m a i n 和 = 之 间 必 须 要 有 空 格 , 否 则 subdomain和=之间必须要有空格,否则 subdomain和=之间必须要有空格,否则subdomain='bb’整个当作变量就报错了
- 负载均衡使用upstream定义的服务器名