windows nignx本地服务器_新手必备教程!5步完成在 Docker 中搭建 Nginx 服务器

本文详细介绍了如何利用Docker来搭建Nginx服务器,包括拉取镜像、创建本地映射文件、配置Nginx文件、启动容器以及日志查看。重点讲解了配置文件的编写,如http、server块,以及如何映射容器内的日志、配置和HTML文件到宿主机。同时,还提到了如何通过proxy_set_header指令进行Host头的转发,并给出了连接到Kong网桥的示例。
摘要由CSDN通过智能技术生成

这篇文章主要介绍了基于docker搭建nginx服务器的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

6fd63041142a181a28e0e5629c6950c0.png
  1. 拉取镜像
$ docker pull nginx
  1. 创建映射文件

比如,我希望在/etc/web-project目录下实现对nginx的日志、配置、html网页进行文件映射,需要创建对应的本地映射文件;

  • 创建映射主目录
## 关于nginx容器映射文件,都保存在/etc/web-project/nginx下$ mkdir nginx
  • 创建对应子文件目录

/etc/web-project/nignx/conf : 挂载容器里面的配置,即nginx.conf

/etc/web-project/nignx/conf.d:挂载容器里面的子配置,即nginx.conf里面include的配置文件

/etc/web-project/nignx/logs :挂载容器里面的代理的日志文件

/etc/web-project/nignx/html :挂载容器里面的界面的访问

## 批量创建$ mkdir -p /data/nginx/{conf,conf.d,html,logs}## 注意:批量创建可能会失败,建议手动直接创建; 
  1. 创建配置文件
  • 路径:/etc/web-project/nginx/conf/nginx.conf
user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.pid;events {    worker_connections  1024;}http {    include       /etc/nginx/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  /var/log/nginx/access.log  main;    sendfile        on;    #tcp_nopush     on;    keepalive_timeout  65;    #gzip  on;    include /etc/nginx/conf.d/*.conf;}
  • 路径:/etc/web-project/conf.d/default.conf
server {      listen       80;      server_name  localhost;        #charset koi8-r;      #access_log  /var/log/nginx/log/host.access.log  main;        location / {          #root   /data/nginx/html;          root   /usr/share/nginx/html;          index  index.html index.htm;          #autoindex  on;          #try_files $uri /index/index/page.html;          #try_files $uri /index/map/page.html;      }        #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   /usr/share/nginx/html;      }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80      #      #location ~ .php$ {      #    proxy_pass   http://127.0.0.1;      #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000      #      #location ~ .php$ {      #    root           html;      #    fastcgi_pass   127.0.0.1:9000;      #    fastcgi_index  index.php;      #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;      #    include        fastcgi_params;      #}        # deny access to .htaccess files, if Apache's document root      # concurs with nginx's one      #      #location ~ /.ht {      #    deny  all;      #}  }
  1. 配置网页html
  • 路径:/etc/web-project/nginx/html/index
    系统时间
  1. 容器启动
docker run --name nginx -d -p 80:80  -v /data/nginx/html:/usr/share/nginx/html  -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf   -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d  nginx:latest### 可以配置网桥,指定ip--network=network-bridge --ip 172.0.0.1
  1. 容器操作
## 查看正在运行的容器$ docker ps## 查看所有容器$ docker ps -a## 查看所有镜像$ docker images
  1. 日志查看

当按照上述步骤进行配置、网页、日志文件映射后,可以比较直观的在宿主机里面看到容器内部的文件数据,因为映射之后,宿主机和容器内映射的文件是一一对应的;

可以在/etc/web-project/nignx/logs文件中查看nginx的所有access.log和error.log日志记录;

  1. 后续Nginx操作

修改对应的nginx.conf、index.html文件来修改容器配置,修改保存后,采用docker restart nginx启动容器

  1. nginx原样转发

需要告诉NGINX将Host头向上游转发给Kong.您可以使用proxy_set_header这样做:

location /api {   ## 代理地址   proxy_pass: http://kong;   ##  host上游转发   proxy_set_header Host $host;}## 或者设置proxy_set_header 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;
  1. nginx映射虚拟
## kong-net :名称为kong-net的桥接网络network$ docker network connect  kong-net nginx-master --ip 172.22.0.9
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值