Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Installation and multi-domain name configuration of Nginx

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Installation and multi-domain name configuration of Nginx? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Nginx installation

Centos6.x yum does not have a nginx package by default

Installation method:

Go to the nginx download page http://nginx.org/en/linux_packages.html#stable and copy the nginx software source installation package for CENTOS 6

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

Install the RPM package yum install nginx-release-centos-6-0.el6.ngx.noarch.rpm-y, and this step only adds the nginx package source

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, status to operate nginx.

Nginx profile

The configuration file for nginx reads the / etc/nginx/nginx.conf file by default.

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

. / nginx-c your conf file location

Can be relative path or absolute path.

If you are not familiar with the environment of the Linux server, you can use the command to quickly find the nginx.conf file:

Sudo find /-name "nginx.conf"

You can also use the command

Sudo nginx-t

To output the profile that is in use:

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

The configuration of nginx consists of directives, and directives consists of simple instructions or block instructions.

Simple instruction: listen 80

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

Http {server {}}

Multi-domain name configuration

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

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

Domain names can also be divided into two-level domain names: admin.525.life.

I point both domain names to my server public network ip 123.123.123.123 in the domain name control panel.

At this point, it is found that both www.525.life and admin.525.life domain name access only correspond to Web programs that use port 80 (the default).

If we want to access port 81 applications can only use:

Www.525.life:81 or admin.525.life:81.

But this is very inconvenient. If we want to remove the port and be able to access it, we need to use Nginx for mapping.

We expect www.525.life to access port 8880 and admin.525.life to access port 8881. Then 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}

This setting will be fine. Map both 8880 and 8881 to port 80 listening.

Use the overload command to make nginx effective:

Sudo nginx-s reload

Restart nginx with the command takes effect:

/ etc/init.d/nginx restart

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

One conf for each domain name

In the above example, we use the method of writing multiple domain names in a file, that is, only one conf is used, and server is constantly added to it. This approach is intuitive, but it is difficult to manage too many domain names.

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

The contents of the admin.conf are:

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

The contents of the www.conf are:

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 / data/nginx/conf/vhost directory.

Then use the incoming command in nginx.conf:

Include / data/nginx/conf/vhost/*.conf

That's it.

It is important to note that this command should be placed in

Http {}

Inside the curly braces.

Because the introduction of include commands is equivalent to all the code introduced is written in nginx.conf.

301 jump

We have noticed that there are many times in life that you can access a website without www, and this can also be achieved through Nginx. As with the above configuration, add another server as follows:

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

Or make a 301 jump.

Server {listen 80 http://www.525.life/$1 permanent; name 525.lifeborn rewrite ^ / (. *) liferewrite}

Add 404 web pages

Add 404 web pages, you can add them directly, such as:

Server {listen 80th serverSecretname www.web126.com; # binds the domain name error_page 404 / 404.html;}

Prohibit direct access to IP

Finally, there is another method to note. It may be necessary to prohibit IP from directly accessing port 80 or to prohibit non-local domain names from binding our IP. In that case, we should

As follows, put it on the top of the first server:

Server {listen 80 default;server_name _; return 403;} this is the answer to the questions about the installation of Nginx and the configuration of multiple domain names. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report