1. 创建三个目录
/mnt/sdb1/nginx/www
静态文件地址目录
/mnt/sdb1/nginx/logs
nginx日志文件地址
/mnt/sdb1/nginx/conf/conf.d
nginx配置文件地址
2. 启动nginx容器
nginx容器启动命令
docker run -p 80:80\
--name nginx_server\
-v /mnt/sdb1/nginx/www:/var/www/html/website\
-v /mnt/sdb1/nginx/logs/:/var/log/nginx\
-v /mnt/sdb1/nginx/conf/conf.d:/etc/nginx/conf.d\
-v /etc/localtime:/etc/localtime
--restart=on-failure
-d nginx
3. 创建配置文件
1) 在/mnt/sdb1/nginx/conf/conf.d创建default.conf文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#导入多模块配置
include /etc/nginx/conf.d/module/*.conf;
location / {
root /var/www/html/website;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/website;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
2) 在/etc/nginx/conf.d下创建upstream.conf文件
配置文件内容为各个模块的服务器地址
upstream base {
server 192.168.2.34:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.2.34:8082 weight=2 max_fails=2 fail_timeout=30s;
}
upstream sso {
server 192.168.2.34:8081 weight=1 max_fails=2 fail_timeout=30s;
}
3) 在/etc/nginx/conf.d下创建module目录
4) 在module目录下创建各模块的配置,文件必须以.conf结尾
比如 创建sso.conf文件,文件内容为
location ~ ^/sso/.*$
{
proxy_pass http://sso;
}
如果添加模块需要先在upstream.conf中添加地址,再在module中创建一个模块对应的配置文件
4. 生配置生效
执行
docker exec nginx_server nginx -t
返回
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
表示配置文件测试通过
执行
docker exec nginx_server nginx -s reload
使配置生效