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

215 阅读2分钟

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


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

1.使用docker安装nginx

docker search nginx

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

docker pull nginx

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

3.查看是否安装成功

docker images

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

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

mkdir -p{html,conf,logs}

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

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

配置文件内容如下:

# 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

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端口

docker ps -a

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

9.重启docker容器

docker restart container-name

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


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