Nginx Configure multiple domain names

Source: Internet
Author: User
Tags server error log

Method One: Multiple. conf method (The advantage is flexible, the disadvantage is that the site more configuration trouble)

Here to configure 2 sites (2 domain names) For example, n sites can increase the adjustment, assuming:

IP Address: 192.168.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/usr/local/nginx/vhosts/
And then add a sentence in/usr/local/nginx/nginx.conf. Include the configuration file created by step 2 (with *)
Restart Nginx

1, open the/usr/local/nginx/nginix.conf file, add in the appropriate location include the above 2 files included in

User www www.

Worker_processes 1;

# Main Server error log

Error_log/usr/local/nginx/log/nginx/error.log;

Pid/usr/local/nginx/nginx.pid;

Events {

Worker_connections 51200;

}

# 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/usr/local/nginx/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/nginx/vhosts/*;

}

2. Create the vhosts directory under/usr/local/nginx

Mkdir/usr/local/nginx/vhosts

3, in the/usr/local/nginx/vhosts/to create a name for the example1.com.conf file, the following content in handcuffs

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/usr/local/nginx/vhosts/to create a name for the example2.com.conf file, the following content in handcuffs

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;
}
}

5, restart Nginx

/etc/init.d/nginx restart
Method Two: Dynamic Directory method (advantage is convenient, each domain name corresponds to a folder, the disadvantage is not flexible)

This simple approach is more important than creating a vhost.conf profile for each domain, just add the following in the existing configuration file:

# Replace This port with the ' right one ' for your requirements
# change this port based on your needs
Listen 80; #could also be 1.2.3.4:80 can also be 1.2.3.4:80 form
# multiple hostnames seperated by spaces. Replace these as.
# Multiple host names can be separated by spaces, and of course this information needs to be changed according to your needs.
server_name star.yourdomain.com *.yourdomain.com http://www.*.yourdomain.com/;
#Alternately: _ *
#或者可以使用: _ * (Details see this wiki other pages)
root/path/to/webroot/$host;
Error_page 404 http://yourdomain.com/errors/404.html;
Access_log Logs/star.yourdomain.com.access.log;
Location/{
root/path/to/webroot/$host/;
Index index.php;
}

# Serve static files directly

# Direct support for static files (from configuration seems to be not directly supported AH)
Location ~* ^.+. (jpg|jpeg|gif|css|png|js|ico|html) $ {
Access_log off;
Expires 30d;
}
Location ~. php$ {
# By all means use a different server for the fcgi processes if you need to
# If necessary, you can set different service information for different fcgi processes
Fastcgi_pass 127.0.0.1:yourfcgiporthere;
Fastcgi_index index.php;
Fastcgi_param script_filename/path/to/webroot/$host/$fastcgi _script_name;
Fastcgi_param query_string $query _string;
Fastcgi_param Request_method $request _method;
Fastcgi_param Content_Type $content _type;
Fastcgi_param content_length $content _length;
Fastcgi_intercept_errors on;
}
Location ~/.ht {
Deny all;
}

Finally, attach another two-level domain name matching method

Bound Domain Name
server_name *.abcd.com;
Get host Name
if ($host ~* (. *). *). (.*))
{
Set $domain $;
}
Definition Directory
Root html/abc/$domain/;
Location/
{
Root html/abcd/$domain;
Index index.html index.php;

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.