Nginx Multi-Site Configuration method Collection _nginx

Source: Internet
Author: User
Tags server error log hosting vps
So let's get started:
1, for our site to create a configuration file
I do this, in the Nginx configuration file conf directory to create a special storage VirtualHost directory, named Vhosts_conf, you can put the virtual directory configuration all here. Create a configuration file called vhosts_modoupi_websuita.conf in it and open it, we're going to configure it here, and write it inside:
Copy Code code as follows:

server {
Listen 80; #监听的端口号
server_name websuita.com; #域名
#access_log Logs/host.access.log Main;
Location/{
Root X:/wnmp/www/websuita; #站点的路径
Index default.php index.php index.html index.htm;
#站点的rewrite在这里写
Rewrite ^/(\w+) \.html$/$1.php;
Rewrite ^/(\w+)/(\w+) $/$1/$2.php;
}
#错误页的配置
Error_page 404/error.html;
Error_page 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, site a configuration, the same method, do WEBSUITB configuration, here I named Vhosts_modoupi_websuitb.conf, directly on the code
Copy Code code as follows:

server {
Listen 80; #监听的端口号
server_name websuitb.com; #域名
#access_log Logs/host.access.log Main;
Location/{
Root X:/WNMP/WWW/WEBSUITB; #站点的路径
Index default.php index.php index.html index.htm;
#站点的rewrite在这里写
Rewrite ^/(\w+) \.html$/$1.php;
Rewrite ^/(\w+)/(\w+) $/$1/$2.php;
}
#错误页的配置
Error_page 404/error.html;
Error_page 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;
}
}

In this way, the configuration of the two sites is OK.
2, in the Nginx main configuration file, contains the configuration files of these two sites.
We open the nginx.conf file under the Conf directory, it is easy to do, as long as in http{...} Segment, enter the following code:
Copy Code code as follows:

#包含所有的虚拟主机的配置文件
Include x:/wnmp/nginx/conf/vhosts_conf/*.conf;

In this way, Nginx's multi-site configuration is done, how to open the browser test it ~

The second method:
When we have a VPS host, in order not to waste the powerful VPS resources (compared to the shared host more than 1000 sites crowded on a machine), there are often want to let VPS do something ideas, silver can not be white flowers AH:). It's a good idea to place multiple websites or blogs, but how can you configure a Web server to place multiple sites/blogs on a single VPS? How do I access multiple sites/domains through one IP? This is the virtual hosting feature supported by most Web servers. This will describe how to configure virtual hosting with Nginx step-by-step.
Nginx is a small and efficient Web server, developed by the Russian programmer Igor Sysoev, Nginx, although small, but not a bit weak, and other Web servers to support virtual hosting, that is, an IP corresponding to multiple domain names to support multi-site access, Like an IP corresponding to a site, so it is "virtual". You want to put how many sites under an IP to put how much, as long as the hard disk is big enough on the line.
Here to configure 2 sites (2 domain names) For example, n sites can increase the adjustment, assuming:
IP Address: 202.55.1.100
Domain 1 example1.com placed in/www/example1
Domain 2 example2.com placed in/www/example2
The basic ideas and steps for configuring Nginx virtual hosting are as follows:
Example1.com 2 sites, example2.com to nginx accessible directory/www/
Create a Nginx profile example1.com.conf,example2.com.conf for each site and place the configuration file in/etc/nginx/vhosts/
And then add a sentence in/etc/nginx.conf. Include the configuration file created by step 2 (with *)
Restart Nginx
Specific process
The following is a specific configuration process:
1. Create the vhosts directory under/etc/nginx
1
Mkdir/etc/nginx/vhosts
2, in the/etc/nginx/vhosts/to create a name for the example1.com.conf file, the following content in handcuffs
Copy Code code 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 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, in the/etc/nginx/vhosts/to create a name for the example2.com.conf file, the following content in handcuffs
Copy Code code 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 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, add in the appropriate location include the above 2 files included in
Copy Code code 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 all the configuration files for the virtual host
include/usr/local/etc/nginx/vhosts/*;
}

5, restart Nginx
The third method:
A server needs to run multiple sites, if only to resolve the domain name to the server IP is not, access to different domain names are nginx Default Web site. To correspond separately, you need to set up vhost in the Nginx.

I am using LNMP one-click installation Package (http://www.lnmp.org/) installation of the nginx+mysql+php environment, for other compiled nginx estimate configuration files and installation directory will be different, I modify the appropriate oh, huh
Edit the/usr/local/nginx/conf/nginx.conf and remove the server's parameters.
Copy Code code 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;
}
And then create a Vhost folder in the/usr/local/nginx/, which creates the corresponding configuration files for each domain name.
This is simple, just copy the previous server configuration content to create the corresponding conf file in the OK.
Copy Code code 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 here that if you are using a first level domain name, you need to specify the domain name without the WWW prefix in the server configuration, otherwise the access jb51.net will be defined to the default site rather than the Www.jb51.net
server_name www.jb51.net;
server_name jb51.net;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.