Installation and multi domain configuration of nginx

Time:2021-7-16

1、 Nginx installation

Centos6. X Yum has no nginx package by default
Installation method:
Go to nginx download pagehttp://nginx.org/en/linux_pac…, copy the nginx software source installation package of CentOS 6
Run command: WGethttp://nginx.org/packages/cen…
Install the RPM package Yum install nginx-release-centos-6-0.el6.ngx.noarch.rpm – Y. in fact, this step only adds the software package source of nginx
Execute Yum install nginx – y to install nginx.

Nginx is installed as a Linux service by default, so you can use service nginx, start, stop, restart, try restart, reload, force reload and status to operate nginx.

2、 Configuration file

The configuration file of nginx reads / etc / nginx / nginx.conf by default.
Of course, you can also modify the path of conf to use
. / nginx – C your conf file location
It can be relative path or absolute path.
If you are not familiar with the Linux server environment, 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 in use:
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 composed of directives, which are composed of simple instructions or block instructions
Simple command: listen 80;
Block instructions are contained by {}. Block instructions can also contain multiple simple instructions and block instructions
http {

server {
}

}

3、 Multi domain configuration

We all know that if you set the IP corresponding to the domain name in the domain name management control panel, you can only set it to IP, not to the port in detail. If multiple web applications are deployed on a server and started on different ports, nginx can be used for mapping.

For example, I have a domain name www.525.life.

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

I point these two domains to my server IP 123.123.123.123 in the domain name control panel.

At this time, we found that the domain name access of www.525.life and admin.525.life only correspond to the web program using port 80 (the default).

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

Www.525.life: 81 or

admin.525.life:81。

But it’s very inconvenient. We need to use nginx for mapping if we want to remove the port and access it.

We expect www.525.life to visit port 8880 and admin.525.life to visit port 8881. Then it can be set 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

}

That’s it. Map both 8880 and 8881 to the 80 port monitor.

Use the overload command to make nginx work:

Sudo nginx – s reload (use this instead: Service nginx reload)

Use the command to restart nginx to take effect:

/etc/init.d/nginx restart

In this way, you can use www.525.life to access port 8880 and admin.525.life to access port 8881.

4、 How to write a conf for each domain name

In the above example, we use the method of writing multiple domain names in one file, that is, only one conf is used, and server is constantly added in it. This way is very intuitive, but the domain name is not easy to manage.
Nginx supports the usage introduced, that is, we can create a new conf file in other places first. The information of the server recorded in the conf file is as follows:
The contents in admin.conf are as follows:
server
{

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

}
The contents of www.conf are as follows:
server
{

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

}
Admin.conf and www.conf are placed in the directory of / data / nginx / conf / Vhost.
Then use the Import command in nginx.conf
include /data/nginx/conf/vhost/*.conf;
That’s it.
It should be noted that this command should be placed in the
http{
}
In curly brackets.
Because the introduction of the include command is equivalent to all the imported code written in nginx. Conf.

5、 301 jump

We notice that many times in our life we can visit a website without WWW, which can also be realized through nginx. Just like the above configuration, add another server as follows:

server
{

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

}

Or jump 301

server
{

listen 80;
server_name 525.life;
rewrite ^/(.*) http://www.525.life/$1 permanent;

}

6、 Add 404 pages

You can add 404 web pages directly, such as:
server
{
listen 80;
server_ name www.web126.com; # Bind domain name
error_page 404 /404.html;
}

7、 Prohibit direct IP access

Finally, there is another way to note that it may be necessary to prohibit IP direct access to port 80 or prohibit non local domain names from binding our IP. In this case, we should
The following processing can be performed on the first server:
server{
listen 80 default;
server_name _;
return 403;
}

Source:http://blog.csdn.net/zzq90050…