The installation of Nginx and the implementation of the multi domain configuration method

  • 2020-05-15 03:38:12
  • OfStack

Nginx installation

centos6.x yum does not have nginx packages by default

Installation method:

To the download page nginx http: / / nginx org/en/linux_packages html # stable, copying CENTOS 6 nginx source software installation package

Run command: wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

Install rpm package yum install nginx - release - centos - 6-0. el6. ngx. noarch. rpm - y, this step actually just joined nginx package source

yum install nginx-y is ready to install nginx.

nginx is installed as Linux service by default, so service nginx start, stop, restart, try-restart, reload, force-reload, status can be used to operate nginx.

Nginx configuration file

nginx default configuration file read/etc nginx/nginx conf file.

Of course, you can also modify the conf path used by using the command:

./nginx -c your conf file location

It could be a relative path or an absolute path.

If you are unfamiliar with the Linux server, you can use the command to quickly find the nginx.conf file:


sudo find / -name "nginx.conf"

You can also use commands


sudo nginx -t 

To output the configuration file being used:


nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful

The configuration of nginx is made up of directives, and directives is made up of simple instructions or block instructions

Simple instructions: listen 80;

Block instructions are contained by {}, which in turn can contain multiple simple instructions and block instructions:


http {
 server {
 }
}

Multi-domain configuration

We all know that if you set the domain name corresponding to ip in the control panel of domain name administration, you can only set it to ip, not to the port. If one server deploys multiple web applications that are launched using different ports, then Nginx can be mapped.

So let's say I have a domain name www.525.life.

Domain names can also be divided into 2 levels: admin.525.life.

From the domain name control panel, I directed both domains to my server public network, ip 123.123.123.123.

At this point, it is found that the www.525.life and admin.525.life domain name access only corresponds to the Web program using port 80 (the default).

If we want to access port 81 the application can only use:

www.525.life:81 or admin.525.life:81.

But it's inconvenient. So if we want to get rid of the port and make it accessible we need to use Nginx for mapping.

We expect www.525.life to access port 8880 and admin.525.life to access port 8881. You can set it as follows:


 server
{
 listen 80;
 server_name www.525.life;
 location / {
  #....
  proxy_pass http://localhost:8880;
 }
 ##### other directive
}
server
{
 listen 80;
 server_name admin.525.life;
 location / {
  #....
  proxy_pass http://localhost:8881;
 }
 ##### other directive
}

So that's it. The listener that maps both 8880 and 8881 to port 80.

Use the overload command to enable nginx:


sudo nginx -s reload

Restart nginx with the command:


/etc/init.d/nginx restart

This allows you to access port 8880 using www.525.life, and port 8881 using admin.525.life.

Each domain name has 1 conf

In the example above, we used one file and multiple domain names, so we only used one conf, and then added server. This way is very intuitive, but the domain name is not easy to manage.

Nginx supports the introduced usage, that is, we can create a new conf file in other places first. The information of server is recorded in conf file as follows:

admin.conf reads:


server
{
 listen 80;
 server_name admin.525.life;
 location / {
  #....
  proxy_pass http://localhost:8881;
 }
 ##### other directive
}

www.conf says:


server
{
 listen 80;
 server_name www.525.life;
 location / {
  #....
  proxy_pass http://localhost:8880;
 }
 ##### other directive
}

admin. conf and www. conf in/data nginx conf/vhost directory.

Then use the introduction command in nginx.conf:


include    /data/nginx/conf/vhost/*.conf;

Can.

Note that this command should be placed


sudo nginx -t 
0

Inside the curly braces.

Because the introduction of the include command is equivalent to the introduction of all the code written in nginx.conf 1.

301 jump

We've noticed that many times in our lives we can access a website without www, and this can be done with Nginx as well. As per the configuration 1 above, add another server as follows:


sudo nginx -t 
1

Or do a 301 jump


sudo nginx -t 
2

Add 404 pages

Add 404 pages, can also be added directly inside, such as:


server
{
listen 80;
server_name www.web126.com; # Binding domain 
error_page 404 /404.html;
} 

Direct access to IP is prohibited

Finally, there is one more method to be aware of. It may be necessary to prohibit IP from directly accessing port 80 or to prevent the domain name of non-website from binding to our IP
The following processing can be placed on the top 1 server:


sudo nginx -t 
4

Related articles: