本篇内容主要讲解“Nginx怎么配置二级域名”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Nginx怎么配置二级域名”吧!
我的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
实现的方法
1、到托管域名的网站, 添加dns解析, 我的域名 fangyuanxiaozhan.com 托管在阿里云, 我的做法是登录 https://dns.console.aliyun.com/#/dns/domainlist , 添加二级记录
2、我使用的是centos7, Nginx配置文件的默认位置为 /etc/Nginx/Nginx.conf
, 有意思的是, /etc/Nginx/Nginx.conf
内引入了 配置文件夹 /etc/Nginx/conf.d
, 也就是我们可以把 /etc/Nginx/Nginx.conf
中的一些默认配置注释掉, 直接在文件夹 /etc/Nginx/conf.d
中配置多个独立的配置文件.
# for more @R_928_4045@ion 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使配置生效
sudo $(which Nginx) -s stop
开启Nginx
sudo $(which Nginx)
效果展示
到此,相信大家对“Nginx怎么配置二级域名”有了更深的了解,不妨来实际操作一番吧!这里是编程之家网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。