How to configure a secondary domain name in nginx
Today, I will talk to you about how to configure second-level domain names in nginx. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.
My vps has registered three services, which are:
The blog service built by WordPress runs on port 8000 and accesses http://fangyuanxiaozhan.com:8000.
Git service built by Gogs, which runs on port 10080 and accesses http://fangyuanxiaozhan.com:10080
The network disk service built by Nextcloud runs on port 8080 and accesses http://fangyuanxiaozhan.com:10080.
My needs:
1. When accessing the blog service, type http://fangyuanxiaozhan.com directly
When accessing the git service, enter http://git.fangyuanxiaozhan.com directly
When accessing the network disk service, enter http://cloud.fangyuanxiaozhan.com directly
The method of realization
1. Go to the website hosting the domain name and add DNS resolution. My domain name fangyuanxiaozhan.com is hosted in Aliyun. What I do is to log in to https://dns.console.aliyun.com/#/dns/domainList and add a secondary record.
2, I use centos7, the default location of the nginx configuration file is / etc/nginx/nginx.conf, interestingly, the configuration folder / etc/nginx/conf.d is introduced in / etc/nginx/nginx.conf, that is, we can comment out some of the default configuration in / etc/nginx/nginx.conf and configure multiple independent configuration files directly in the folder / etc/nginx/conf.d.
Configuration of / 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;}
Note that on the last line of the configuration file above, include / etc/nginx/conf.d/*.conf; ensures that under / etc/nginx/conf.d/, all configuration files ending in .conf will be introduced and take effect by the main configuration file nginx.conf.
Three new files need to be created under / etc/nginx/conf.d/
Blog.conf (to map port 8000 to port 80 without using second-level domain names)
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 implements mapping from fangyuanxiaozhan.com:8000 to fangyuanxiaozhan.com
Git.conf (to map port 10080 to port 80, using the second-level domain name 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 implements mapping from fangyuanxiaozhan.com:10080 to git.fangyuanxiaozhan.com
Nc.conf (to map port 10080 to port 80, using the second-level domain name 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 implements mapping from fangyuanxiaozhan.com:8080 to cloud.fangyuanxiaozhan.com
Restart nginx to make the configuration effective
Close nginx
Sudo $(which nginx)-s stop
Turn on nginx
Sudo $(which nginx)
Effect display
After reading the above, do you have any further understanding of how to configure a secondary domain name in nginx? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.