nginx web hosting and load balancing

nginx

1. compile and install configuration
/opt/nginx11/html/index.html This is the home page file

2. nginx.conf master profile study
###################################### follows
worker_processes 4 ; Nginx number of work processes, defined according to the number of core cpu
Events {
worker_connections 1024; # connections
}
#http block region, the core web features defined Nginx
HTTP {
the include (keyword) the mime.types (modifiable value);
default_type file application / OCTET-Stream;

# custom log format
log_format main '$ REMOTE_ADDR - $ REMOTE_USER [$ time_local] "$ Request"'
'$ Status $ body_bytes_sent "$ HTTP_REFERER"'
' "$ HTTP_USER_AGENT" "$ HTTP_X_FORWARDED_FOR"';
# open parameter access log function
access_log logs / the access.log main;
the sendfile ON;
#tcp_nopush ON;
#keepalive_timeout 0;
# holding a long connection
keepalive_timeout 65;
# Support gif images and so on compression to reduce network bandwidth
ON gzip;

# this server tag controls the nginx web hosting (web site)
server {
inlet port # 80 is defined nginx port
the listen 80;
# fill in the domain name, the domain name will not write ip address
server_name www.s15rihan.com;
#define encoding
charset UTF-8;
# LOCATION define web access url
# represents the user's request is 192.168.13.79/
LOCATION / {
#root parameter defines the web root directory
root HTML;
# define your personal home page file name
index index. index.htm HTML;
}
# custom error page, client error, an error code is returned 40x series
error_page 404 403 401 400 /404.html;
# 500 series error back-end code representative of an error
error_page 500 502 503 504 /50x.html ;
}
# outside another server {}, and writes the new virtual server 2
Server {
the listen 80;
server_name www.s15oumei.com;
LOCATION / {
root / opt / myserver / oumei; # define virtual host web root
index index.html;
}
}
}

3. Prepare the web root directory of the contents of two virtual hosts
[root @ localhost myserver] # Tree / opt / myserver /
/ opt / myserver /
├── oumei
│ └── index.html write their own content
└── rihan
└── index.html write your own content

4. modify the windows of local tests domain C: \ Windows \ System32 \ drivers \ etc \ hosts file
write the following

192.168.13.79 www.s15rihan.com

192.168.13.79 www.s15oumei.com

Because we do not www.s15oumei.com nor www.s15rihan.com, so to get a domain name in the local test,
do not want to change the dns, then went to Ali goes to buy a domain name ~~~ ~~~ ~~~~~~~~~~

5. then visit two different web site in a browser test

www.s15rihan.com

www.s15oumei.com

 

nginx access log function
1. Turn nginx.conf of log parameters

log_format main '$ REMOTE_ADDR - $ REMOTE_USER [$ time_local] "$ Request"'
'$ Status $ body_bytes_sent "$ HTTP_REFERER"'
' "$ HTTP_USER_AGENT" "$ HTTP_X_FORWARDED_FOR"';
# Enable access logging parameters
access_log logs / access. main log;


2. check the log information access.log

tail -f access.log

 

nginx access refusal
1. nginx.conf added parameter
in the host server {} virtual tag, and then add the location to find the parameters

# When Zhao Yining access 192.168.13.79/ of
LOCATION / {
# parameter is rejected deny
#deny write IP address you want to reject
#deny also refused to support an entire website
deny 192.168.13.33;
root / opt / myserver / Rihan;
index index .html;
}


Optimization nginx error page
1. nginx.conf modify the configuration parameters in
the presence of the virtual host definition s1540x.html web root directory
error_page 404 /s1540x.html;


nginx reverse proxy
1. middleman
2. Derivative
3. second-hand dealers

4. cattle train

vpn is forward proxy
Chinese users on their machines, using a vpn ip address, and then through the IP address of vpn and external communication

 

nginx reverse proxy function (comes with a reverse proxy functionality, born Erdaofanzi)
1. Experimental environment ready to
prepare two servers are installed nginx software


nginx1 192.168.13.79 as a web server (understood as a train ticket)

nginx2 192.168.13.24 as a reverse proxy server (cattle)

users to access the cattle through the browser (proxy)
browser to access 192.168.13.24> 192.168.13.79


2. Add a reverse proxy server configuration

 

 

nginx load balancing

The concept of the cluster: a bunch of servers to do one thing


1. Experimental ready to
prepare three computers

nginx1 192.168.13.121 as a result of nginx load balancer as long as I visited the load balancer, see the page, in the end from

nginx2 192.168.13.24 web services to provide a page

nginx3 192.168.13.79 web services to provide a page

 

2. configure two nginx web pages
192.168.13.24 prepare a written index.html Hello, my machine is 192.168.13.24
192.168.13.79 prepare a written index.html older brother, I was 192.168.13.79

and then start two a Web service nginx

3. prepare a load balancer nginx 192.168.13.121 machine, modify nginx.conf
written as follows
define a pool of load balancing, load balancing algorithm
scheduling algorithm outlined
polled individually assigned to different chronological backend server (default)
weight WRR, the larger the weight, the higher the probability assigned to the access
ip_hash hash result for each request assigned by the IP access, access from the same IP thus fixed to a back-end server
url_hash against the access URL to allocate request hash result, each URL is directed to the same back-end server
least_conn minimum number of links, the fewer the number of links on the distribution machine

Polling (not configured, the default polling)

2.weight weights (priority)

3.ip_hash configuration, ip hash allocated according to the client, and the weight can not be used with

{s15webserver upstream
ip_hash;
Server 192.168.13.79;
Server 192.168.13.24;
}

and add virtual host reverse proxy configuration, the user's request, and forwards to the load balancer server pool

{Server
the listen 80;
# when I request from 192.168.13.121, which go> virtual host
server_name 192.168.13.121;

#charset koi8-r;

logs #access_log / main host.access.log;
# core configuration, in this, a parameter can proxy_psss
LOCATION / {
proxy_pass HTTP: // s15webserver;
#root HTML;
#index index.html index.htm;
}

}


4. Start nginx load balancer service

5. On the client windows in test access, load balancer 192.168.13.121, view the results of the request distribution

 


Today's job:
1. build a good nginx page
2. nginx to build two virtual hosts can access www.s15oumei.com www.s15rihan.com
3. complete nginx load balancing configuration

 

Guess you like

Origin www.cnblogs.com/duhong0520/p/11687525.html