docker nginx实现一个主机部署多个站点

用户故事:发布两个网站

现状:只有一个云服务器(不想多买)

任务:

1.使用php-fpm镜像开启php-fpm应用容器

拉取php-fpm镜像:

sudo docker pull bitnami/php-fpm

先运行,创建container,copy配置文件(也可不用)

sudo docker run -d --rm --name myFpm bitnami/php-fpm
sudo docker cp  myFpm:/opt/bitnami/php /home/ubuntu/share/php-fpm

Tip: --rm 参数,当容器停止后自动删除。

创建php-fpm容器:

sudo docker run -d --name myFpm -v /var/www/html:/usr/share/nginx/html -v /home/ubuntu/share/php-fpm/php:/opt/bitnami/php bitnami/php-fpm

其中/var/www/html是宿主机存放站点文件的路径(自定义的),/usr/share/nginx/html是为nginx服务器项目默认的路径。

Tip:     -d : 该参数为后台运行之意 

            -v : 指定宿主机与容器的映射关系

2.使用nginx:alpine镜像开启nginx应用容器

拉取nginx:alpine镜像:

sudo docker pull nginx:alpine

先运行,创建container,copy配置文件

sudo docker run --rm -d --name myNginx nginx:alpine

复制配置文件

sudo docker cp myNginx:/etc/nginx/nginx.conf /home/ubuntu/share/nginx/conf

其中/etc/nginx/nginx.conf是nginx中默认的配置文件,/home/ubuntu/share/nginx/conf宿主机文件路径(自定义的)

做之前只复制了这个文件,后面在操作的时候,其实还需要操作nginx容器中/etc/nginx/conf.d/default.conf配置文件

当然后面用了一个笨办法,在宿主机上编辑好,接着copy到docker 容器中:

sudo docker cp default.conf myNginx:/etc/nginx/conf.d/default.conf

下面启动docker nginx容器:

sudo docker run -d --name myNginx -p 80:80 -v /var/www/html:/usr/share/nginx/html -v /home/ubuntu/share/nginx/conf/nginx.conf:/etc/nginx/nginx.conf nginx:alpine

其中/var/www/html是宿主机存放站点文件的路径(自定义的),/usr/share/nginx/html是为nginx服务器项目默认的路径。

3. 查看容器是否都正常启动

sudo docker ps

4. 配置nginx

查看php-fpm的IP信息

sudo docker inspect myFpm | grep "IPAddress"

结果(这里引用了网络上的一个结果,当然我也得到了同样的结果,这里实在不想再去截图了^_^)

由此得到了php-fpm容器的Ip地址,接着修改nginx中的default.conf文件

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

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

    #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   172.17.0.2:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$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;
    #}
}

这里要注意fastcgi_pass和fastcgi_param这两个参数,其中fastcgi_pass用的就是php-fpm的ip和服务的端口172.17.0.2:9000,

fastcgi_param我是参考这个帖子中的设置的,还是很感谢这么多的大神的无私分享:

下面是我的nginx.conf文件:

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

其中user 参数,我改成了root,这个要参考你发布站点路径的权限,一定要注意。

当然这些都做好了之后,我用index.php测试的时候,还是遇到了403forbidden的错误,排查了很久,发现实在default.conf里面的index参数中我没有添加 index.php,添加后一切正常。具体会引发nginx 403 forbidden的原因还有很多,如果想了解更多可以看下这个帖子。

记得重启nginx!!!!!!!!!!!!!!

5.多站点配置

这里参考了很多帖子,有些帖子是利用nginx的代理,还在研究,比如有这么配置的:

worker_processes  1;
events {
  worker_connections  1024;
}
http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  server
    {
      listen 80;
      server_name www.dcssn.com;
      location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://47.92.69.112:8080;
      }
    }
 
  server
    {
      listen 80;
      server_name www.xhxf119.com;
      location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://47.92.69.112:8081;
      }
    }
}

我这里是参考的这种配置,我觉得前期实现相对简单些:

# 向配置目录添加如下两个配置并建立相应目录和测试的index.html文件,
#向host文件添加web1.com和web2.com两个域名 
# conf1.conf 
server { 
    listen 80; 
    server_name web1.com; 
    location / { 
        root /usr/share/nginx/html/web1; 
        index index.html index.htm; 
    } 
} 

# conf2.conf 
server { 
    listen 80; 
    server_name web2.com; 
    location / { 
        root /usr/share/nginx/html/web2; 
        index index.html index.htm; 
    } 
} 

# 如果要添加php脚本请添加如下,注意fastcgi_pass必须是php的inspect的ip信息,
#注意修改SCRIPT_FILENAME目录路径 
location ~ \.php$ { 
     fastcgi_pass 172.17.0.2:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
     fastcgi_param SCRIPT_NAME $fastcgi_script_name; 
     include fastcgi_params; 
}

下面是我自己的其中的一个站点的配置:

server {
         listen 80;
         server_name www.XXXXXXX.com;
         location / {
                      root /usr/share/nginx/html/XXXXXX.com;
                      index index.html index.php;
         }

         location ~ \.php$ {
                     fastcgi_pass 172.17.0.2:9000;
                     fastcgi_index index.php;
                     fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/XXXXXX.com$fastcgi_script_name;
                     fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                     include fastcgi_params;
         }

}

至于另一种多站点的方式,后面再做研究吧 ~ 还希望能接着配置https,加油!fighting!!!!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值