Home  >  Article  >  Operation and Maintenance  >  How to configure second-level domain name in nginx

How to configure second-level domain name in nginx

王林
王林forward
2023-05-27 17:37:292041browse

My vps has three services, which are:

  1. The blog service built by wordpress, running on port 8000, accessed via http ://fangyuanxiaozhan.com:8000

  2. git service built by gogs, running on port 10080, access method http://fangyuanxiaozhan.com:10080

  3. The network disk service built by nextcloud runs on port 8080. The access method is http://fangyuanxiaozhan.com:10080

##My needs:

  1. 1. When accessing the blog service, directly enter http://fangyuanxiaozhan.com

  2. When accessing the git service, Directly enter http://git.fangyuanxiaozhan.com

  3. to access the network disk service, directly enter http://cloud.fangyuanxiaozhan.com

Implementation method

1. Go to the website hosting the domain name and add dns resolution. My domain name fangyuanxiaozhan.com is hosted on Alibaba Cloud. My method is to log in to https://dns.console.aliyun.com /#/dns/domainlist, add secondary records

How to configure second-level domain name in nginx

2. I am using centos7, and the default location of the nginx configuration file is

/etc/nginx/nginx .conf , interestingly, /etc/nginx/nginx.conf introduces the configuration folder /etc/nginx/conf.d , that is, we can put # Comment out some default configurations in ##/etc/nginx/nginx.conf and configure multiple independent configuration files directly in the folder /etc/nginx/conf.d.

How to configure second-level domain name in nginx

/etc/nginx/nginx.conf

Configuration <pre class="brush:plain;"># 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 &amp;#39;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &amp;#39; &amp;#39;$status $body_bytes_sent &quot;$http_referer&quot; &amp;#39; &amp;#39;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&amp;#39;; 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; }</pre>Pay attention to the last line of the above configuration file,

include /etc/ nginx/conf.d/*.conf;

ensures that under /etc/nginx/conf.d/, all configuration files ending with .conf will be included in the main configuration file nginx .conf is introduced and takes effectYou need to create three new files under

/etc/nginx/conf.d/

How to configure second-level domain name in nginxblog.conf (realizes port 8000 mapping to port 80, without using the second-level domain name)

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 realizes mapping fangyuanxiaozhan.com:8000 to fangyuanxiaozhan.com

git.conf (Realizing port 10080 to port 80, using the second-level domain name

git

)<pre class="brush:bash;">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; } }</pre>git.conf implements mapping of fangyuanxiaozhan.com:10080 to git.fangyuanxiaozhan.com

nc.conf (realizes port 10080 mapping to port 80, using the second-level domain name

cloud

)<pre class="brush:bash;">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; } }</pre>git.conf realizes mapping fangyuanxiaozhan.com:8080 to cloud.fangyuanxiaozhan.com

Restart nginx to make the configuration take effect

Close nginx

sudo $(which nginx) -s stop

Open nginx

sudo $(which nginx)

Effect display

How to configure second-level domain name in nginx

How to configure second-level domain name in nginx

The above is the detailed content of How to configure second-level domain name in nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete