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

Nginx how to configure multiple sites with one IP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to configure multiple sites with one IP of Nginx". In daily operation, I believe many people have doubts about how to configure multiple sites with one IP of Nginx. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to configure multiple sites with a Nginx IP". Next, please follow the editor to study!

Environment

I happen to have an Aliyun student machine on hand. Use it before it expires.

Operating system: centos7

Php-fpm

Nginx

Initialization

It is not very difficult to build a php-fpm+nginx environment on centos. There are many posts on the Internet. Just install the software that needs to be installed as explained above.

Catalogue list

After installation, the configuration file is usually located in the / etc/nginx directory, and the contents on my side are as follows:

The nginx.conf inside is the default configuration file. Let's take a simple look at the general content.

Generally speaking, we will not modify the contents of the nginx.conf file unless there is a special need. Now, we can use this file as our reference item.

Personally, I think there are several more important configuration items:

Http: this node represents a nginx, which is our chief manager. It can have multiple server configuration items inside, representing multiple sites.

Although the include / etc/nginx/conf.d/*.conf; line looks humble, it is extremely important for configuring multiple sites. We'll talk about that later.

Log_format: the format of the log, which will be reflected in the action of nginx logging.

Access.log: the first word in this option is main, which is the log format just defined by log_format. The same applies to error.log later.

Server node: a server node that contains the configuration of a site, where the content of the same name will override the configuration of the http node, so the priority for a site is relatively higher.

Include / etc/nginx/default.d/*.conf is not difficult to see that this configuration appears inside the server node, so it still works for the configuration of this site. This default.d directory stores features that are universal to all server nodes, and the purpose of its existence is to allow us to write less repetitive configuration content. Instead, it is extracted and put into a general directory.

The content of location will be discussed later.

Configuration

Let's get down to business and see how to configure multiple sites on a single ip. So first of all, we need to create a few folders and then serve as our multiple sites.

Site preparation

Create two folders under the home/www directory, one is blog and the other is forum, and then put an index.php in it. Note that the content can be well distinguished.

Add Profil

As we have just learned, if there are multiple sites, there are actually multiple server nodes, and the http node is introduced through include / etc/nginx/conf.d/*.conf in nginx.conf, so we only need to create the configuration files we need to be specific to a particular site in the / etc/nginx/conf.d/ directory.

Note: note that the suffix is .conf, otherwise according to the rules of include, the relevant configuration files cannot be introduced correctly.

You can do the same as I do, as follows:

The fastcgi_param script_filename on the left is misspelled. It should be $document_root. It hasn't been changed in the picture. Just make it clear.

After adding the configuration file, restart nginx. The command to restart on centos is as follows:

Systemctl restart nginx

If you have a debian linux, you can also use:

Service nginx restart

At this point, you can access it through the browser to see if our configuration item is successful in the end.

In this way, multiple sites are successfully configured on a single ip through different ports.

Difficult and miscellaneous

When I first came into contact with nginx, I was really confused about this configuration file. At that time, in my impression, php+apache was the golden combination. Unexpectedly, as the time of contact with nginx became longer, the combination of php-fpm+nginx opened my eyes.

On the road of configuration, there will always be a few problems that have been bothering me. I would like to sum up the more important issues that I personally think.

Fastcgi_pass

Within the location of the server node, there is such a configuration item. It's very confusing.

Location ~\ .php$ {root / home/wwwroot; fastcgi_pass 127.0.0.1 home/wwwroot; fastcgi_pass 9000; # fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; # fastcgi_pass unix:/tmp/php-cgi.sock; try_files $uri / index.php = 404; fastcgi_split_path_info ^ (. +\ .php) (/. +) $; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params;}

From the above configuration file, we also see a lot of different configurations for it, so how to choose?

I found such an answer on the Internet, and I felt that people spoke very well. If you are interested, please take a look at the following article. Https://segmentfault.com/q/1010000004854045

To sum up, there are the following points:

There are two ways to communicate between processes in nginx+php-fpm.

One is tcp, the other is unix domain socket.

Tcp: the format is ip: Port, which can be used across servers.

Unix domain socket does not go through the network and can only be used in scenarios where both nginx and php-fpm are on the same server.

So, for us, how do we choose? The answer depends on the configuration of php-fpm. There are two ways:

Method 1:

Php-fpm.conf: listen = 127.0.0.1:9000nginx.conf:fastcgi_pass 127.0.0.1 127.0.0.1:9000nginx.conf:fastcgi_pass 9000

Method 2:

Php-fpm.conf: listen = / tmp/php-fpm.socknginx.conf: fastcgi_pass unix:/tmp/php-fpm.sock

Here php-fpm.sock is a file, generated by php-fpm, the type is srw-rw--, exactly how to write this path, or depends on the location of the sock file generated by your local php-fpm.

Both of these can be successfully combined with php-fpm and nginx. What's the difference? the original words of the boss should be more convincing.

Unix domain socket can be used for two unrelated processes. It is a widely used ipc mechanism at present. For example, the communication between x window server and gui program is through unix domain socket. This kind of communication occurs in the kernel of the system and does not spread in the network. Unix domain socket and long connections can avoid the problem of frequent creation of tcp short connections, which leads to too many time_wait connections. For the two programs of inter-process communication, the flow of unix domain socket will not go to the tcp layer, but will communicate directly in the form of files and stream socket. If it's tcp socket, you need to go to the ip tier. For different servers, tcp socket goes even more.

So, next time you don't have to worry about it, just keep the terms of php-fpm.conf and nginx.conf consistent.

At this point, on the "Nginx an IP how to configure multiple sites" on the end of the study, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report