假如项目通过自动化部署在了docker容器里面:教程在这里vue使用docker+node+nginx+linux自动化部署_1024小神的博客-CSDN博客
怎么将nginx通过域名绑定到这个容器呢?
例如我们将一个二级域名绑定second.1024shen.com/#/ 到我们的服务
我们需要先在域名解析里面添加我们的这个二级域名:主机记录就是二级域名,像普通的www其实也是二级域名的范畴了,哈哈哈哈
然后添加一个新的nginx配置文件:一般都在/etc/nginx/sites-available这个里面:
添加的配置内容如下:
需要修改两个地方:一个二级域名,一个容器的端口
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80;
listen [::]:80;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
# root /var/www/second;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
# second.1024shen.com就是二级域名
server_name second.1024shen.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# http://127.0.0.1:9090/这个就是容器的本地ip:端口
proxy_pass http://127.0.0.1:9090/;
# try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
然后还要为这个配置创建一个软连接:
linux创建软连接命令: ln -s [dir1] [dir2] ,dir1是真实的文件夹/文件夹,dir2是dir1的软链接。
将软连接放在/etc/nginx/sites-enabled下面,软连接相当于快捷方式,连接到second文件地址:
然后使用nginx -t 测试一下nginx配置是否正确:
nginx -t
如果没有问题,就重新加载配置:
nginx -s reload
恭喜你,现在已经成功了,打开二级域名访问一下: