Nginx Virtual Host Configuration tutorial

Source: Internet
Author: User
Tags varnish

Nginx can use the server block to set multiple virtual hosts. In the server segment, use the server_name and listen commands to bind domain names and ports. For example:

The code is as follows: Copy code

Server {
Listen 80;
Server_name www.111cn.net;

Location /{
Root hzhui;
Index index.html;
 }
}


The preceding configuration specifies a virtual host www.111cn.net. The above configuration may not work well in some nginx versions. In this case, all requests are processed by the first server, if you want to configure multiple servers, you only need to configure them in the middle of server.

The following describes how to configure a second-level domain name

The code is as follows: Copy code

Server {
# You can replace this item as needed. The recommended Port is port 80.
Listen 80 [default | default_server]; # cocould also be 1.2.3.4: 80

# Separate multiple domain names with spaces
Server_name star.yourdomain.com * .yourdomain.com;
# Web Directory
Root/PATH/TO/WEBROOT/$ host;
Error. If the page is not found, it is changed to 404.html.
Error_page 404 errors/404.html;
# Diary record
Access_log logs/star.yourdomain.com. access. log;
# Homepage index, from left to right, higher than the front optimization level
Index. php index.html index.htm;

# Provide static files
Location ~ *. (Jpg | jpeg | gif | css | png | js | ico | html) $ {
Access_log off;
Expires max;
}
# Run php-fpm on nginx
Location ~ . Php $ {
Include fastcgi_params;
Fastcgi_intercept_errors on;
# By all means use a different server for the fcgi processes if you need
Fastcgi_pass 127.0.0.1: YOURFCGIPORTHERE;
}

Location ~ /. Ht {
Deny all;
}
}

All of the above are single hosts. If I wantConfigure dual-hostNext, let's look at the method.


On the official Nginx website, it is said to be proxying without caching. However, it seems that there is cache. In fact, the number of external HTTP connections is much larger than the number of connections from proxy to varnish at the same time.

Another consideration of using Nginx virtual hosts is to prepare a simple server load balancer for the second cache server that may need to be added after the access traffic increases.

The code is as follows: Copy code

Upstream mysite.cn {

Server 127.0.0.1: 8080;

# Server 192.168.11.1: 80; possible second cache in the future

}  

Server


The first virtual host for varnish and Nginx running status monitoring

The code is as follows: Copy code

Listen 80;

Server_name cache.mysite.cn;

Index index.htm index.html index. php;

Root/web/htdocs;

Location /{

Index infov.txt;

Allow 192.168.0.0/16;

Deny all;

}  

Location/status {

Stub_status on;

Access_log off;

Allow 192.168.0.0/16;

Deny all;

}   

}  

Server


The second virtual host, which is directed to varnish

The code is as follows: Copy code

Listen 80;

Client_max_body_size 50 M;

Server_name .mysite.cn;

Index index.htm index.html index. php;

Root/web/htdocs;

Location /{

Proxy_pass http://mysite.cn;

Proxy_redirect off;

Proxy_set_header Host $ host;

Proxy_set_header X-Real-IP $ remote_addr;

Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;

Client_max_body_size 50 m;

Client_body_buffer_size 256 k;

Proxy_connect_timeout 10;

Proxy_send_timeout 15;

Proxy_read_timeout 15;

Proxy_buffer_size 4 k;

Proxy_buffers 4 32 k;

Proxy_busy_buffers_size 64 k;

Proxy_temp_file_write_size 64 k;

}  

# Location ~ . *. Php? $

#{

# Include conf/fcgi. conf;

# Fastcgi_pass 127.0.0.1: 10080;

# Fastcgi_index 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.