Docker中安装nginx与自定义配置

nginx简介

Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务,由于项目中需要处理负载均衡

在Docker下载Nginx镜像
# 搜索nginx
docker search ngnix 
# 拉去远程的nginx镜像
docker pull ngnix
# 查看镜像
docker images
创建挂载目录

我们这里需要挂载可手动修改配置文件的nginx,而不是全部封装在docker容器中运行的。

mkdir -p /nginx/{conf,conf.d,html,logs}
编写nginx.conf配置文件

编写我们自定义的配置文件放在conf文件夹中
配置文件内容如下

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  120.77.96.184;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://pic; 
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    upstream pic{
                server 120.77.96.184:8090 weight=1;
                server 120.77.96.184:8091 weight=1;
    }

}

upstream pics是我们配置的负载均衡服务器切换,我们通过监听请求的80端口,来分配到8090和8091端口的服务器请求。

启动容器
docker run --name mynginx -d -p 80:80  -v /nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /nginx/logs:/var/log/nginx -d docker.io/nginx
查看容器启动列表

docker ps -a

启动项目测试效果

你可能感兴趣的:(Docker中安装nginx与自定义配置)