nginx collection of multi site configuration methods
- 2020-05-06 12:12:53
- OfStack
So here we go:
1. Create the configuration file
for our site Here's what I did: I created a directory dedicated to VirtualHost in the conf directory of nginx's configuration file, vhosts_conf, where I can put all the configuration of the virtual directory. Inside, create a configuration file named vhosts_modoupi_websuitA.conf and open it
So now we have the configuration of A for the site, and the same way, we have the configuration of websuitB, which I'm going to call vhosts_modoupi_websuitB.conf, which goes directly to
Thus, the configuration of the two sites is OK.
2. In the nginx master profile, include the profiles for these two sites.
We open conf directory under nginx.conf file, easy to do, as long as http{... Enter the following code:
So, the nginx multi-site configuration is done, how to open the browser to test it ~
second method:
when we have an VPS host, in order not to waste VPS's powerful resources (compared to sharing more than 1000 sites crowded on a single machine), we often have the idea of VPS to do something, not for nothing :). It's a good idea to have multiple sites or blogs, but how do you configure the web server to have multiple sites/blogs on one VPS server? How do I access multiple sites/domains from one IP? This is the virtual hosting functionality that most web servers support. Here is a step-by-step description of 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. It can support virtual hosting as other web servers. You can put as many sites under an IP as you want, as long as the hard drive is big enough.
Here, the configuration of 2 sites (2 domain names) for example, n sites can be adjusted accordingly, assuming:
IP address: 202.55.1.100
Domain name 1 example1.com is placed on /www/example1
Domain name 2 example2.com is placed on /www/example2
The basic idea and steps to configure nginx virtual hosting are as follows:
Put the two sites example1.com, example2.com into a directory accessible to nginx /www/
For each site to create a nginx example1 configuration file. The com. conf, example2. com. conf, and put the configuration file in/etc nginx/vhosts/
Then add include to /etc/ nginx.conf to include all the configuration files created in step 2 (with *)
Restart nginx
Here's how:
1. Create vhosts directory
under /etc/nginx 1
mkdir /etc/nginx/vhosts
2, in the/etc nginx vhosts/create a name for example1 com. conf file, put the following kao
inside
3, in the/etc nginx vhosts/create a name for example2 com. conf file, put the following kao
inside
4. Open /etc/ nginix.conf file and add include in the corresponding location to include the above two files in
5. Restart Nginx
third method :
needs to run multiple websites on one server. If you only resolve the domain name to IP of server, it is not possible. If you visit different domains, you will open the default website of nginx. To correspond, you need to set vhost in nginx.
I am lnmp is a key installation package (http: / / www. lnmp. org /) installed nginx + mysql + php environment, to other nginx estimate configuration file and compile the installation directory are different, their discretion modify oh, ha ha
Edit/usr/local/nginx/conf/nginx conf, remove server parameters.
Then add vhost definition: copyright
include /usr/local/nginx/vhost/*.conf;
}
Again in/usr local nginx/vhost folder, to create the corresponding domain configuration file.
This is as simple as copying the previous server configuration into the corresponding conf file you created.
To note here, if you use a top-level domain, you need not specified in the configuration of server www prefix of the domain name, or access jb51. net is defined to the default site rather than www. jb51. net
server_name www.jb51.net;
server_name jb51.net;
1. Create the configuration file
for our site Here's what I did: I created a directory dedicated to VirtualHost in the conf directory of nginx's configuration file, vhosts_conf, where I can put all the configuration of the virtual directory. Inside, create a configuration file named vhosts_modoupi_websuitA.conf and open it
server {
listen 80; # The port number to listen to
server_name websuitA.com; # The domain name
#access_log logs/host.access.log main;
location / {
root X:/wnmp/www/websuitA; # Path to the site
index default.php index.php index.html index.htm;
# The site rewrite Write here
rewrite ^/(\w+)\.html$ /$1.php;
rewrite ^/(\w+)/(\w+)$ /$1/$2.php;
}
# Configuration of the error page
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;
}
}
So now we have the configuration of A for the site, and the same way, we have the configuration of websuitB, which I'm going to call vhosts_modoupi_websuitB.conf, which goes directly to
server {
listen 80; # The port number to listen to
server_name websuitB.com; # The domain name
#access_log logs/host.access.log main;
location / {
root X:/wnmp/www/websuitB; # Path to the site
index default.php index.php index.html index.htm;
# The site rewrite Write here
rewrite ^/(\w+)\.html$ /$1.php;
rewrite ^/(\w+)/(\w+)$ /$1/$2.php;
}
# Configuration of the error page
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;
}
}
Thus, the configuration of the two sites is OK.
2. In the nginx master profile, include the profiles for these two sites.
We open conf directory under nginx.conf file, easy to do, as long as http{... Enter the following code:
# Contains configuration files for all virtual hosts
include X:/wnmp/nginx/conf/vhosts_conf/*.conf;
So, the nginx multi-site configuration is done, how to open the browser to test it ~
second method:
when we have an VPS host, in order not to waste VPS's powerful resources (compared to sharing more than 1000 sites crowded on a single machine), we often have the idea of VPS to do something, not for nothing :). It's a good idea to have multiple sites or blogs, but how do you configure the web server to have multiple sites/blogs on one VPS server? How do I access multiple sites/domains from one IP? This is the virtual hosting functionality that most web servers support. Here is a step-by-step description of 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. It can support virtual hosting as other web servers. You can put as many sites under an IP as you want, as long as the hard drive is big enough.
Here, the configuration of 2 sites (2 domain names) for example, n sites can be adjusted accordingly, assuming:
IP address: 202.55.1.100
Domain name 1 example1.com is placed on /www/example1
Domain name 2 example2.com is placed on /www/example2
The basic idea and steps to configure nginx virtual hosting are as follows:
Put the two sites example1.com, example2.com into a directory accessible to nginx /www/
For each site to create a nginx example1 configuration file. The com. conf, example2. com. conf, and put the configuration file in/etc nginx/vhosts/
Then add include to /etc/ nginx.conf to include all the configuration files created in step 2 (with *)
Restart nginx
Here's how:
1. Create vhosts directory
under /etc/nginx 1
mkdir /etc/nginx/vhosts
2, in the/etc nginx vhosts/create a name for example1 com. conf file, put the following kao
inside
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, in the/etc nginx vhosts/create a name for example2 com. conf file, put the following kao
inside
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 /etc/ nginix.conf file and add include in the corresponding location to include the above two files in
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
third method :
needs to run multiple websites on one server. If you only resolve the domain name to IP of server, it is not possible. If you visit different domains, you will open the default website of nginx. To correspond, you need to set vhost in nginx.
I am lnmp is a key installation package (http: / / www. lnmp. org /) installed nginx + mysql + php environment, to other nginx estimate configuration file and compile the installation directory are different, their discretion modify oh, ha ha
Edit/usr/local/nginx/conf/nginx conf, remove server parameters.
server
{
listen 80;
server_name www.wifizoo.net;
index index.html index.htm index.php;
root /tmp/wwwroot; This article from the
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 vhost definition: copyright
include /usr/local/nginx/vhost/*.conf;
}
Again in/usr local nginx/vhost folder, to create the corresponding domain configuration file.
This is as simple as copying the previous server configuration into the corresponding conf file you created.
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;
}
To note here, if you use a top-level domain, you need not specified in the configuration of server www prefix of the domain name, or access jb51. net is defined to the default site rather than www. jb51. net
server_name www.jb51.net;
server_name jb51.net;