Docker构建nginx并挂载外部配置文件

1.首先pull下载nginx镜像包

docker pull nginx

2.查看nginx镜像里面配置文件、日志等文件的具体位置,便于最后覆盖这些路径

docker run -i -t nginx /bin/bash 进入nginx容器

找到镜像中nginx.conf配置文件路径

nginx.conf :/etc/nginx/nginx.conf

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/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"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}

default.conf : /etc/nginx/conf.d/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;
index 1.html;
}

#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;
}
}

log文件夹:/var/log/nginx

<html>

<head>

<title>Mynginx</title>

</head>

<body>

<h1>

my nginx!

</h1>

</body>

</html>

3.创建本地挂载文件,并创建文件放入对应文件夹,详细配置根据上一步文件修改

mkdir -p /home/app/nginx/conf nginx.conf存放位置

mkdir -p /home/app/nginx/conf.d default.conf存放位置

mkdir -p /home/app/nginx/html html存放位置

mkdir -p /home/app/nginx/logs log文件存放位置(自动生成log文件只需创建文件夹即可)

4.创建容器并且挂载文件和文件夹

docker run –name mynginx -d -p 8501:80 -v /home/app/nginx/html:/usr/share/nginx/html -v /home/app/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/app/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /home/app/nginx/logs:/var/log/nginx nginx

docker run –name mynginx -d -p 8501:80

-v /home/app/nginx/html:/usr/share/nginx/html

-v /home/app/nginx/conf/nginx.conf:/etc/nginx/nginx.conf

-v /home/app/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf

-v /home/app/nginx/logs:/var/log/nginx nginx

挂载的目标目录或者文件路径要与第二步里面已经找出来的镜像中的路径保持一致

5.docker ps 查看有没有启动成功

6.http://IP:端口 打开网页查看

Docker构建nginx并挂载外部配置文件插图

=========================================================================

nginx常见配置:

负载均衡:

1.在http部分添加

upstream myserver{ //upstream是关键字 myserver是自定义的需要负载均衡服务的名称
server ip1:端口1;
server ip2:端口2;
}

2.在server部分添加

location /fz/ {
proxy_pass http://myserver;
}

反向代理:

在server部分添加一个location即可:下图是代理了天猫首页实例(若在路径上配置了正则就不能代理具体的url地址,会引起nginx启动错误

location /tm/ {
proxy_pass https://www.tianmao.com/;
}

原文链接:https://blog.csdn.net/weixin_46712666/article/details/121944995?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522f73403363c3c095ee9c4301db7a9bfd4%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=f73403363c3c095ee9c4301db7a9bfd4&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-21-121944995-null-null.nonecase&utm_term=%E7%BB%BF%E8%81%94nas

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享