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

How to implement Multi-site configuration with Nginx

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

Share

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

Editor to share with you how to achieve multi-site configuration of Nginx, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

When we have a VPS host, in order not to waste the powerful resources of VPS (compared to sharing hosts with more than 1000 sites huddled on a single machine), we often have the idea that we want VPS to do something, money can't be wasted. It's a good idea to place multiple websites or blogs, but how do you configure a web server to place multiple websites / blogs on a VPS? How to access multiple sites / domain names through one IP? This is the virtual hosting feature supported by most web servers. Here we will describe how to configure virtual hosting with nginx step by step.

Nginx is a small and efficient web server, developed by Russian programmer Igor Sysoev. Although nginx is small, it is not weak at all. It can support virtual hosting like other web servers, that is, an IP corresponds to multiple domain names to support multi-site access, just like an IP corresponding to a site, so it is "virtual". You can put as many sites under an IP as you want, as long as the hard drive is big enough.

Here, take the configuration of 2 sites (2 domain names) as an example, n sites can be increased and adjusted accordingly, assuming:

IP address: 202.55.1.100 domain name 1 example1.com on / www/example1 domain name 2 example2.com on / www/example2

The basic ideas and steps for configuring nginx virtual hosting are as follows:

Put the 2 sites example1.com and example2.com into the directory / www/ that nginx can access.

Create a nginx profile example1.com.conf,example2.com.conf for each site and put the profile in / etc/nginx/vhosts/

Then add an include to / etc/nginx.conf to include all the configuration files created in step 2 (with the * sign)

Restart nginx

Concrete process

The following is the specific configuration process:

1. Create a vhosts directory under / etc/nginx

Mkdir / etc/nginx/vhosts

2. Create a file named example1.com.conf in / etc/nginx/vhosts/ and copy the following

Server {listen 80; server_name example1.com www. Example1.com; access_log / www/access_ example1.log main; location / {root / www/example1.com; index index.php index.html index.htm;} error_page 500 502 503 504 / 50x.html location = / 50x.html {root / usr/share/nginx/html;} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 location 9000 location ~ .php ${fastcgi_pass 127.0.0.1 fastcgi_pass 9000; fastcgi_index index.php Fastcgi_param SCRIPT_FILENAME / www/example1.com/$fastcgi_script_name; include fastcgi_params;} location ~ / .ht {deny all;}}

3. Create a file named example2.com.conf in / etc/nginx/vhosts/ and copy the following

Server {listen 80th serverkeeper name example2.com www. Example2.com; access_log / www/access_ example1.log main; location / {root / www/example2.com; index index.php index.html index.htm;} error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root / usr/share/nginx/html;} # pass the PHP scripts to FastCGI server listening on 127.0.1 location 9000 location ~ .php ${fastcgi_pass 127.0.0.1 location 9000; fastcgi_index index.php Fastcgi_param SCRIPT_FILENAME / www/example2.com/$fastcgi_script_name; include fastcgi_params;} location ~ / .ht {deny all;}}

4. Open the / etc/nginix.conf file and add include in the appropriate location to include the above two files

User nginx; worker_processes 1; # main server error log error_log / var/log/nginx/error.log; pid / var/run/nginx.pid; events {worker_connections 1024;} # main server config http {include mime.types; default_type application/octet-stream; log_format main'$remote_addr-$remote_user [$time_local] $request''"$status" $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"; sendfile on # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; gzip on; server {listen 80; server_name_; access_log / var/log/nginx/access.log main; server_name_in_redirect off; location / {root / usr/share/nginx/html; index index.html;}} # contains the configuration files include / usr/local/etc/nginx/vhosts/*; for all virtual hosts

5. Restart Nginx

/ etc/init.d/nginx restart

The above is all the contents of the article "how to achieve multi-site configuration in Nginx". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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