docker安装nginx

编程

1、在Docker下载Nginx镜像

docker pull nginx

docker images

2、创建挂载目录

mkdir -p /data/nginx/{conf,conf.d,html,logs}

3、编写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 182.254.161.54;

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 182.254.161.54:8088 weight=5;

server 182.254.161.54:8089 weight=5;

}

}

说明:182.254.161.54是将要转发过去的服务器ip地址,8088和8089分别对应web容器端口

4、启动容器

docker run --name mynginx -d -p 80:80  -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx

说明:第一个80是客户访问的端口,第二个是本身映射的端口

5、查看启动的容器

docker ps 

先前已经在Docker部署两个tomcat,一个是8088端口,另一个是8089端口,并进入两个容器里编写了简单的页面

此时访问182.254.161.54:80就随机自动转发的不同服务器端口上了,实现负载均衡的功能

 

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

可能在最后你放我项目地址后,会发现nginx样式丢失先现象,不用慌,按以下处理即可:

user nobody;

worker_processes 4;

events{

worker_connections 1024;

}

http{

upstream xd-project{

server 47.52.68.xx:8080;

server 45.40.205.xx;

}

server{

listen 80;

location / {

proxy_pass http://xd-project;

}

location ~ .* {

proxy_pass http://xd-project;

proxy_set_header Host $http_host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

}

在server里面加入location相关内容即可。

以上是 docker安装nginx 的全部内容, 来源链接: utcz.com/z/510967.html

回到顶部