nginx Multi-site Configuration Method Collection _nginx_ Home of Scripts

nginx multi-site configuration method collection

Updated: June 28, 2011 21:24:04 Author:
The multi-site setup for nginx is actually very similar to apache, let's say we already have two domains, www.websuitA.com and www.websuitB.com. And the two domain names have been mapped to the server whose IP address is 192.168.1.1.
So here we go:
1. Create a profile for our site
Here's what I did: I created a dedicated VirtualHost directory in the nginx configuration file conf directory called vhosts_conf where I could put all the virtual directory configurations. Create a configuration file named vhosts_modoupi_websuitA.conf in it and open it. We will do the configuration here and write in it:
Copy codeThe code is as follows:

server {
listen 80; # Listening port number
server_name websuitA.com; # Domain name
#access_log logs/host.access.log main;
location / {
root X:/wnmp/www/websuitA; # The path to the site
index default.php index.php index.html index.htm;
# Site rewrite here
rewrite ^/(\w+)\.html$ /$1.php;
rewrite ^/(\w+)/(\w+)$ /$1/$2.php;
}
# Error page configuration
error_page 404 /error.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root X:/wnmp/www/websuitA;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

This is done, the configuration of site A, the same method, do the configuration of websuitB, here I named vhosts_modoupi_websuitB.conf, directly on the code
Copy codeThe code is as follows:

server {
listen 80; # Listening port number
server_name websuitB.com; # Domain name
#access_log logs/host.access.log main;
location / {
root X:/wnmp/www/websuitB; # The path to the site
index default.php index.php index.html index.htm;
# Site rewrite here
rewrite ^/(\w+)\.html$ /$1.php;
rewrite ^/(\w+)/(\w+)$ /$1/$2.php;
}
# Error page configuration
error_page 404 /error.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root X:/wnmp/www/websuitB;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

This way, the configuration of the two sites is OK.
2. The main nginx configuration file contains the configuration files for these two sites.
We open the nginx.conf file in the conf directory, which is easy to do, just in http{... } Enter the following code:
Copy codeThe code is as follows:

# Contains configuration files for all virtual hosts
include X:/wnmp/nginx/conf/vhosts_conf/*.conf;

In this way, nginx multi-site configuration is done, how to open the browser to test it

The second method:
When we have a VPS host, in order not to waste the powerful resources of VPS (compared to the shared host of more than 1000 sites crowded on a machine), often have the idea of wanting VPS to do something, silver can not be spent in white ah :). Hosting multiple websites or blogs is a great idea, but how do you configure a web server to host multiple websites/blogs on a single VPS? How to access multiple sites/domains from one IP? This is the virtual hosting feature that most web servers support. This section describes step by step how to configure virtual hosting with nginx.
nginx is a small and efficient web server, developed by Russian programmer Igor Sysoev. Although nginx is small in size, its functions are not weak at all, and it can support virtual hosting like other web servers. That is, one IP corresponds to multiple domain names to support multiple site access, just like one IP corresponds to one site, so it is "virtual". You can put as many sites as you want under an IP, as long as the hard drive is big enough.
Here, configure two sites (two domain names) as an example, n sites can be added and adjusted accordingly, assuming:
IP address: 202.55.1.100
Domain 1 example1.com is placed on /www/example1
Domain 2 example2.com is placed on /www/example2
The basic ideas and steps for configuring nginx virtual hosting are as follows:
Put the two sites example1.com and example2.com in a directory that nginx can access /www/
Create an nginx configuration file, example1.com.conf, example2.com.conf, for each site, and place the configuration file in /etc/nginx/vhosts/
Then add an include to /etc/nginx.conf to include all the configuration files created in step 2 (marked with an asterisk).
Restart nginx
Concrete process
The following is the specific configuration process:
1. Create the vhosts directory in /etc/nginx
1
mkdir /etc/nginx/vhosts
2. Create a file named example1.com.conf in /etc/nginx/vhosts/ and copy the following contents to it
Copy codeThe code is as follows:

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:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1: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 to it
Copy codeThe code is as follows:

server {
listen 80;
server_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.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1: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 position to include the above two files
Copy codeThe code is as follows:

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 configuration files for all virtual hosts
include /usr/local/etc/nginx/vhosts/*;
}

5. Restart Nginx
The third method:
A server needs to run multiple websites, if only the domain name resolution to the server IP is not possible, access to different domain names open are nginx default website. To do this, you need to set vhost in nginx.

I used lnmp one-click installation package (http://www.lnmp.org/) to install the nginx+mysql+php environment, for other compiled nginx configuration file and installation directory will be different, modify their own appropriate oh, ha ha
Edit the/usr/local/nginx/conf/nginx. Conf, get rid of the parameters of the server.
Copy codeThe code is as follows:

server
{
listen 80;
server_name www.wifizoo.net;
index index.html index.htm index.php;
root /tmp/wwwroot; This article is from
location ~ .*\.(php|php5)? $
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
} copyright
location /status {
stub_status on;
access_log off;
}
copyright
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)? $
{
expires 12h;
}

log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /home/wwwroot/logs/access.log access;
}

Then add the vhost definition: copyright
include /usr/local/nginx/vhost/*.conf;
}
Create a vhost folder in /usr/local/nginx/ and create configuration files for each domain name.
This is simple, just copy the previous server configuration content into the corresponding created conf file.
Copy codeThe code is as follows:

server
{
listen 80;
server_name www.jb51.net;
server_name jb51.net;
index index.html index.htm index.php;
root /tmp/wwwroot/meituge;

location ~ .*\.(php|php5)? $
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
} copyright
location /status {
stub_status on;
access_log off;
}
copyright

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
copyright

location ~ .*\.(js|css)? $
{
expires 12h;
}
#log_format access '$remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" $http_x_forwarded_for';
#access_log /home/wwwroot/logs/access.log access;
}

Note that if you are using the first level domain name, you will need to specify the domain name without the www prefix in the server configuration, otherwise access to jb51.net will be defined to the default site instead of www.jb51.net
server_name www.jb51.net;
server_name jb51.net;

Related article

  • CentOS系统rpm安装Nginx和配置

    CentOS rpm installation and configuration of Nginx

    Hello everyone, this article is mainly about the installation of Nginx and configuration of CentOS system rpm, interested students come and have a look, if it is helpful to you, remember to collect it, convenient for the next browse
    2021-12-12
  • 负载均衡下的webshell上传+nginx解析漏洞的过程

    The process of webshell upload +nginx vulnerability resolution under load balancing

    This article mainly introduces the load balancing under the webshell upload +nginx analysis vulnerability, first introduced the load balancing under the webshell upload four difficulties and environment building tutorial, interested friends follow the small series to see it
    2024-02-02
  • nginx开启https配置之后网页无法访问的问题处理解决

    nginx Web pages cannot be accessed after https is enabled

    Recently, after the newly purchased server deployed nginx, the front-end project was deployed in the previous way and https was configured, and the access page could not be displayed. This article mainly introduces how to solve the problem that the web page could not be accessed after nginx opened the https configuration. It has certain reference value
    2023-11-11
  • Nginx添加ipv6模块以及遇到问题解决方案详解(亲测有效)

    Nginx added ipv6 module and encountered problem solution details (personal test effective)

    IPV4 is increasingly scarce,ipv6 has been slowly on the agenda, to be popularized in the country, the use of nginx configuration ipv6 that is certain, the following article mainly introduces to you about Nginx add ipv6 module and the solution to the problem, the need of friends can refer to
    2022-09-09
  • nginx 代理服务器配置双向证书验证的方法

    The nginx proxy server configures the method of bidirectional certificate authentication

    This article mainly introduces the nginx proxy server configuration two-way certificate verification method, Xiaobian feel very good, now share to you, also give you a reference. Let's take a look
    2019-02-02
  • nginx命令参数用法详细介绍

    This section describes the usage of nginx command parameters

    This article mainly introduces the nginx command parameter usage details of the relevant information, I hope that through this article can help you understand and apply this part of the knowledge, the need of friends can refer to
    2017-08-08
  • Nginx limit 限制访问模块的方法

    Nginx limit Specifies the method to restrict access to a module

    This article mainly introduces the method of Nginx limit to restrict access to modules, Xiaobian feel very good, now share with you, but also give you a reference. Let's take a look
    2018-03-03
  • Nginx Rewrite使用场景及代码案例详解

    Nginx Rewrite use scenario and code case details

    This article mainly introduces Nginx Rewrite usage scenarios and code cases in detail, the article introduces the example code in great detail, which has certain reference value for everyone's study or work, and the friends who need it can refer to it
    2020-08-08
  • Nginx部署SpringBoot项目的实现

    Nginx deploys the SpringBoot project implementation

    This article mainly introduces the implementation of Nginx deployment SpringBoot project, the article introduces very detailed through the example code, for everyone's study or work has a certain reference learning value, the need for friends to study together with the small series below
    2023-03-03
  • Nginx启用gzip压缩的方法示例

    Example of how Nginx enables gzip compression

    This article mainly introduces Nginx to enable gzip compression method example, Xiaobian feel very good, now share to you, also give you a reference. Let's take a look
    2018-07-07

Latest comments