我的vps掛了三個服務(wù), 分別是:
-
wordpress搭建的博客服務(wù), 運行于8000端口, 訪問方式 http://fangyuanxiaozhan.com:8000
-
gogs搭建的git服務(wù), 運行于10080端口, 訪問方式 http://fangyuanxiaozhan.com:10080
-
nextcloud搭建的網(wǎng)盤服務(wù), 運行于8080端口, 訪問方式 http://fangyuanxiaozhan.com:10080
我的需求:
-
1.訪問博客服務(wù)時, 直接輸入 http://fangyuanxiaozhan.com
-
訪問git服務(wù)時, 直接輸入 http://git.fangyuanxiaozhan.com
-
訪問網(wǎng)盤服務(wù)時, 直接輸入 http://cloud.fangyuanxiaozhan.com
實現(xiàn)的方法
1、到托管域名的網(wǎng)站, 添加dns解析, 我的域名 fangyuanxiaozhan.com 托管在阿里云, 我的做法是登錄 https://dns.console.aliyun.com/#/dns/domainlist , 添加二級記錄
?
2、我使用的是centos7, nginx配置文件的默認(rèn)位置為?/etc/nginx/nginx.conf?, 有意思的是,?/etc/nginx/nginx.conf?內(nèi)引入了 配置文件夾?/etc/nginx/conf.d?, 也就是我們可以把?/etc/nginx/nginx.conf?中的一些默認(rèn)配置注釋掉, 直接在文件夾?/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結(jié)尾的配置文件, 都會被主配置文件?nginx.conf?引入并生效
在?/etc/nginx/conf.d/?下面需要新建三個文件
?
blog.conf (實現(xiàn)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實現(xiàn)了fangyuanxiaozhan.com:8000映射到 fangyuanxiaozhan.com
git.conf (實現(xiàn)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實現(xiàn)了fangyuanxiaozhan.com:10080映射到 git.fangyuanxiaozhan.com
nc.conf (實現(xiàn)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實現(xiàn)了fangyuanxiaozhan.com:8080映射到 cloud.fangyuanxiaozhan.com
重啟nginx使配置生效
關(guān)閉nginx
sudo?$(which?nginx)?-s?stop
開啟nginx
sudo?$(which?nginx)
效果展示
?
?
?