Docker Nginx实现一个主机部署多个站点操作
由于在某站租赁的虚拟机即将到期且续费成本较高(约200多元),决定将其迁移至新购买的阿里云服务器。文中使用的域名均为真实域名,旨在提供实际参考案例。
当前阿里云服务器已运行一个站点,通过域名 www.dcssn.com 可直接访问。目标是新增另一个域名 www.xhxf119.com 指向同一主机,并根据访问域名的不同,将流量分发至不同的后端服务。
首先需要进行基础的网络配置。
确保所有相关域名的DNS解析记录均指向该阿里云主机的公网IP地址。
接下来配置后端服务的端口映射。
为 www.dcssn.com 对应的服务启动容器,并将宿主机的8080端口映射到容器的80端口,命令示例:docker run -p 8080:80 weian。
为 www.xhxf119.com 对应的服务启动容器,并将宿主机的8081端口映射到容器的80端口,命令示例:docker run -p 8081:80 xinhua。
验证后端服务可用性,此时通过访问 www.dcssn.com:8080 和 www.xhxf119.com:8081 均应能正常看到服务页面。
随后编写Nginx的配置文件以实现域名路由。
nginx.conf
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;
}
}
}
将编写好的配置文件保存至宿主机的指定目录,例如 /host/path/ 目录下。
启动Nginx容器,挂载配置文件并映射80端口,命令示例:docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d -p 80:80 nginx。
完成上述步骤后,即可直接通过 www.dcssn.com 或 www.xhxf119.com 访问对应服务,无需输入端口号。

补充知识:Docker配置Nginx支持多个子域名对应不同的项目目录。
未来可考虑引入反向代理分发机制以优化架构。
具体的PHP与Nginx配合配置细节,请参考博主博客中的相关搭建文章。
进入Nginx配置目录,命令:cd /root/nginx/conf/conf.d。
创建或编辑配置文件,命令:vim runoob-test-php.conf。
runoob-test-php.conf文件内容主要配置子域名与本地目录的映射关系。
server {
listen 80;
server_name www.liuyuanshan.top;
location / {
#proxy_pass http://106.52.36.65:80;
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name message.liuyuanshan.top;
location / {
root /usr/share/nginx/html/message/;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/message/$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name wordpress.liuyuanshan.top;
location / {
root /usr/share/nginx/html/wordpress/;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/wordpress/$fastcgi_script_name;
include fastcgi_params;
}
}
修改配置后需要重启Nginx容器使配置生效。
执行重启命令:docker restart ngixn。
以上即为Docker结合Nginx实现单主机多站点部署的完整操作流程,希望能为您提供参考,欢迎支持本站。