美文网首页
Docker 简单搭建Nginx

Docker 简单搭建Nginx

作者: 楚长铭 | 来源:发表于2020-02-10 16:35 被阅读0次

    搭建环境

    • Mac

    拉取镜像

    docker  pull nginx
    

    启动容器

    docker run -d -p 20000:80 --name nginx-web -v /WorkSpace/dokcer/nginx/www:/usr/share/nginx/html -v /WorkSpace/dokcer/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /WorkSpace/dokcer/nginx/logs:/var/log/nginx nginx
    
    • 端口20000指向nginx内部的的80端口
    • 本地创建www,conf,logs三个文件夹,在conf文件夹下创建nginx.conf文件

    配置文件

    
    worker_processes  1;
     
     
     
    events {
        worker_connections  1024;
    }
     
     
    http {
        include       mime.types;
        default_type  application/octet-stream;
     
        sendfile        on;
        keepalive_timeout  65;
    
        charset utf-8;
    
    
        server{
            listen       80;
            server_name  localhost;
            
            location / {
               proxy_pass http://ip:端口号;
    
               proxy_set_header X-Forwarded-Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header REMOTE-HOST $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
            }
        }
    
    }
    
    • 这样的效果就是访问localhost:20000就是访问http://ip:端口号这个地址

    域名映射

    server {
        listen       80;
        server_name  test.com;
    
        location / {
          proxy_pass http://ip:端口号;
    
          proxy_set_header X-Forwarded-Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header REMOTE-HOST $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }
    
    • 域名映射与localhost相比就是改了server_name
    • 但是本机需要配置host文件,mac是在 /etc文件夹下的host文件,在末尾添加
    ip地址  www.test.com
    

    相关文章

      网友评论

          本文标题:Docker 简单搭建Nginx

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