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!!!!

你使用Docker部署多个Vue应用到Nginx上。首先,确保你的Vue应用已经被打包成dist文件夹。接下来,需要进行以下几个步骤: 1. 使用以下命令拉取Nginx的镜像:docker pull nginx。这个命令会将Nginx下载到你的本地环境中。 2. 将dist文件夹放到/opt/nginx目录下,与其他文件夹处于同一层级。 3. 执行以下命令来运行Nginx容器并将dist文件夹映射到Nginx中: sudo docker run --name nginx01 -p 8080:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf:/etc/nginx -v /opt/nginx/dist:/opt/dist -d nginx。 这个命令会创建一个名为nginx01的Nginx容器,并将本地的8080端口映射到容器的80端口,同时将dist文件夹映射到Nginx容器中的相应目录。 通过这个步骤,你就可以在Nginx部署多个Vue应用了。每个应用都可以通过不同的端口进行访问。你可以根据需要,使用类似的命令创建多个Nginx容器来部署更多的Vue应用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [docker部署nginx和vue(可以部署多个nginx和vue)](https://blog.csdn.net/qq_35498696/article/details/124484135)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Docker结合Jenkins部署vue项目](https://download.csdn.net/download/weixin_38672807/14885048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值