nginx配置二级域名

您所在的位置:网站首页 nginx二级域名用不同目录吗 nginx配置二级域名

nginx配置二级域名

2024-01-29 08:28| 来源: 网络整理| 查看: 265

为了不让域名fangyuanxiaozhan.com闲置, 作者又买了个国内的虚拟主机(VPS)的ip为111.230.254.173, 用wordpress开了个博客网站, 由于vps的空间很大, 我就开了个私有网盘服务, 由于日常开发需要用到git, 但又不想公开代码, 我又开了个私有git服务

我的vps挂了三个服务, 分别是:​WordPress搭建的博客服务, 运行于8000端口, 访问方式 http://fangyuanxiaozhan.com:8000 ​Gogs搭建的git服务, 运行于10080端口, 访问方式 http://fangyuanxiaozhan.com:10080 ​Nextcloud搭建的网盘服务, 运行于8080端口, 访问方式http://fangyuanxiaozhan.com:10080 我的需求:1.访问博客服务时, 直接输入 http://fangyuanxiaozhan.com ​访问git服务时, 直接输入 http://git.fangyuanxiaozhan.com ​访问网盘服务时, 直接输入 http://cloud.fangyuanxiaozhan.com 实现的方法到托管域名的网站, 添加DNS解析, 我的域名fangyuanxiaozhan.com托管在阿里云, 我的做法是登录https://dns.console.aliyun.com/#/dns/domainList, 添加二级记录

我使用的是centos7, nginx配置文件的默认位置为/etc/nginx/nginx.conf, 有意思的是,/etc/nginx/nginx.conf内引入了 配置文件夹/etc/nginx/conf.d, 也就是我们可以把/etc/nginx/nginx.conf中的一些默认配置注释掉, 直接在文件夹/etc/nginx/conf.d中配置多个独立的配置文件.

/etc/nginx/nginx.conf的配置# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; }

注意上述配置文件的最后一行, include /etc/nginx/conf.d/*.conf;保证了/etc/nginx/conf.d/下,所有以.conf结尾的配置文件, 都会被主配置文件nginx.conf引入并生效

在/etc/nginx/conf.d/下面需要新建三个文件

blog.conf (实现8000端口映射到80端口, 不使用二级域名)server { listen 80; server_name fangyuanxiaozhan.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://0.0.0.0:8000; } }

blog.conf实现了fangyuanxiaozhan.com:8000映射到 fangyuanxiaozhan.com

git.conf (实现10080端口映射到80端口, 使用二级域名git)server { listen 80; server_name git.fangyuanxiaozhan.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://0.0.0.0:10080; } }

git.conf实现了fangyuanxiaozhan.com:10080映射到 git.fangyuanxiaozhan.com

nc.conf (实现10080端口映射到80端口, 使用二级域名cloud)server { listen 80; server_name cloud.fangyuanxiaozhan.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://0.0.0.0:8080; } }

git.conf实现了fangyuanxiaozhan.com:8080映射到 cloud.fangyuanxiaozhan.com

重启nginx使配置生效关闭nginxsudo $(which nginx) -s stop开启nginxsudo $(which nginx)效果展示

如果你也希望搭建以上服务, 可以参考我的往期文章:云盘服务: 简单三步, 搭建全平台私有同步网盘 git服务: 用gogs搭建属于自己的git网站 博客服务: docker进阶 一键安装wordpress

自从使用了docker, 发现很多服务的搭建都是很简单的, 但随着服务的增多, 二级域名的作用性就显现出来了, nginx可以很方便的解决二级域名的配置问题,掌握了nginx配置二级域名的技术, 我们就可以优雅的开启各种服务了~



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3