作为一个后端,你应该会的最基本的运维技巧(四)~

这是我参与更文挑战的第5天,活动详情查看: 更文挑战


一、Ubuntu服务器使用docker容器安装nginx并且配置其反代让外部可访问

1.使用docker安装nginx

1
sql复制代码docker search nginx

显示如下:
安装nginx
2.下载nginx

1
复制代码docker pull nginx

显示如下:
下载nginx
说明一哈:如果需要下载指定版本的nginx,需要在后面跟上版本号,如果不加版本号,默认为最新版本。

3.查看是否安装成功

1
复制代码docker images

显示如下:
安装成功效果图
显示以上效果就证明安装成功啦!接下来我们来启动docker并且配置。

4.首先建立html、conf、logs文件夹

1
css复制代码mkdir -p{html,conf,logs}

显示如下:
建立文件夹
1)html 作为我们访问入口,文件放在此目录下外部即可访问。
2)conf 配置文件,里面配置了nginx的一些挂载
3)logs 日志,运行docker时会生产日志,方便后期调试和修改

5.将docker的配置文件上传到conf文件目录下,利用scp命令来上传:
blog.csdn.net/weixin_4382…

配置文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
ini复制代码# 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 8081;
root /etc/nginx/html;
location / {
proxy_pass https://www.qycloud.com.cn/api2/user/login/logindata;
}
}
server {
listen 8981;
root /etc/nginx/html;
location / {
proxy_pass https://www.qycloud.com.cn/api2/user/login/logindata;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.zxhtom.store;
root /usr/share/nginx/html;

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

location / {
root /usr/share/nginx/html;
index index.html;
}

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

}

6.启动docker

1
bash复制代码docker run -d -p 80:80 --name dyjnginx -v /opt/soft/nginx/html:/usr/share/nginx/html -v /opt/soft/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/soft/nginx/logs:/var/log/nginx nginx

解析:
1)docker run 启动docker
2)-d 后台启动
3)-p 80:80 将容器的 80 端口映射到主机的 80 端口
4)-v /opt/soft/nginx/html:/usr/share/nginx/html
———将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html
5)–name dyjnginx 将docker容器命名为dyjnginx

7.查看docker端口

1
css复制代码docker ps -a

显示如下:
显示如下
8.接下来我使用了scp命令将一个静态页面上传到html目录下,然后输入ip地址及路劲访问,效果如下:
效果图

9.重启docker容器

1
复制代码docker restart container-name

以上就是配置docker的全部内容啦~


点赞收藏加关注不迷路哦~

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%