美文网首页
3.docker安装nginx

3.docker安装nginx

作者: 一个小前端程序员 | 来源:发表于2023-02-25 12:13 被阅读0次

    创建 /home/docker/nginx/conf/nginx.conf 文件及/home/docker/nginx/log文件夹
    其nginx.conf 文件为在原nginx.conf上添加上自定义的一些配置

    mkdir -p  /home/docker/nginx/conf
    touch  /home/docker/nginx/conf/nginx.conf
    mkdir -p  /home/docker/nginx/log
    

    配置nginx.conf

    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       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  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                #这里是nginx docker容器的文件夹,由于映射关系也是服务器的/home/docker/jenkins_home/www
                root   /var/web/www; 
                try_files $uri $uri/ /index.html last;
                index  index.html index.htm;
               # error_page 405 =200 http://$host$request_uri;
            }
    
            #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   html;
            }
       }
    }
    

    运行命令,若没有镜像则会自动pull镜像

    docker run -p 8080:80 -v /home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/docker/nginx/log:/var/log/nginx -v /home/docker/jenkins_home/www:/var/web/www -d docker.io/nginx
    

    其中:

    • -p 8080:80 将8080端口映射至docker的80端口
    • -v /home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 配置映射:服务器中的nginx配置映射至容器的nginx配置中
    • -v /home/docker/nginx/log:/var/log/nginx 日志映射
    • -v /home/docker/jenkins_home/www:/var/web/www 网页文件映射:jenkins打包后的web页面文件夹映射至nginx配置的web文件夹(在配置文件中注意更改 location /{root /var/web/www; ...}),也可以是 /home/docker/jenkins_home/www下面的文件夹但是nginx.conf要对应的进行修改

    访问ip:8080就可以看到了

    相关文章

      网友评论

          本文标题:3.docker安装nginx

          本文链接:https://www.haomeiwen.com/subject/cypfldtx.html