docker笔记1--配置nginx服务器
1、安装步骤
1)docker pull nginx (下载nginx镜像)
2)docker images | grep nginx (查看nginx镜像)
3)启动容器
docker run -p 80:80 --name myweb nginx (容器名称为myweb)
4)新建nginx目录和子目录www、logs、conf,以便于存放文件,拷贝myweb中的default.conf文件到conf中
docker cp myweb:/etc/nginx/conf.d/default.conf ./conf (需要启动容器后才能拷贝)
若不知道容器内各文件分布,可通过docker exec -it 775c7c9ee1e1 /bin/bash 进入myweb的命令行中,其中775c7c9ee1e1 为myweb的CONTAINER ID
5)修改default.conf配置文件
正常情况下,用4)启动nginx它会使用默认的 /etc/nginx/conf.d/default.conf 配置文件,打开默认的网页
原default.conf内容如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #原始html文件位置为:启动容器后该目录下的index.html
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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
通常我们需根据需求修改如下内容,如将root修改为www
listen 80;
server_name localhost;
root /www/html; # 修改后,nginx启动的时候会在该目录(注意:此处为容器内的/www/html目录)下找index.html文件
index index.html index.htm;
6)容器启动命
笔者根据自己的目录做了如下启动命令,最好将其写到一个shell文件中,以免多次输入命令
docker run -p 80:80 --name myweb -v $PWD/www/html:/www/html \
-v $PWD/conf/default.conf:/etc/nginx/conf.d/default.conf -v $PWD/logs:/wwwlogs -d nginx
该shell脚本需要放在nginx目录下,否则使用PWD时候会找不到对应的子目录;
其中-v xxx1:xxx2 即将宿主机中的xxx1映射到docker容器中xxx2
7)执行命令,通过ip访问服务器,如下图所示:
2、常见案例
2.1 nginx 实现端口转发
nginx 可以通过proxy_pass 实现端口转发,以下案例将本机的19121 端口转发到10.120.75.103的19121端口上;
添加配置后 执行 /etc/init.d/nginx reload 即会自动生效;
server {
listen 19121;
server_name milvus-19121;
location / {
proxy_pass http://10.120.75.103:19121;
}
}
2.2 ipv6 导致nginx 启动失败
大部分系统默认不一定支持ipv6,因此需要将 listen [::]:80 注释掉,然后重启即可;
server {
listen 80;
# listen [::]:80;
·····
}
2.3 配置负载均衡
将9000端口负载均衡到10.214.58.67:9001-9004端口
upstream minio-server {
server 10.214.58.67:9001 weight=3;
server 10.214.58.67:9002 weight=3;
server 10.214.58.67:9003 weight=3;
server 10.214.58.67:9004 weight=3;
}
server {
listen 9000;
server_name minio;
# To allow special characters in headers
ignore_invalid_headers off;
# Allow any size file to be uploaded.
# Set to a value such as 1000m; to restrict file size to a specific value
client_max_body_size 0;
# To disable buffering
proxy_buffering off;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_connect_timeout 300;
# Default is HTTP/1, keepalive is only enabled in HTTP/1.1
proxy_http_version 1.1;
proxy_set_header Connection "";
chunked_transfer_encoding off;
proxy_pass http://minio-server; # If you are using docker-compose this would be the hostname i.e. minio
# Health Check endpoint might go here. See https://www.nginx.com/resources/wiki/modules/healthcheck/
# /minio/health/live;
}
}
3、说明
截图系统版本:Ubuntu 16.04.4 Desktop LTS (64-bit), 安装在VM虚拟机上
需要注意的是:当前版本Nginx默认启动文件为 /etc/nginx/conf.d/default.conf,因此将其拷贝到宿主虚拟机conf中后,需要适当修改root位置,也可以不修改,通过命令 -v $PWD/本地宿主虚拟机网页目录:/www/html,将本地网页目录映射到容器/www/html目录,即容器/www/html目录中内容和本地虚拟机对应目录内容相同