A brief introduction to Nginx and the construction of load balancing using Nginx under linux
#在server上添加此upstream节点
upstream mytomcat{
#分权 即访问131与134的次数比例为1比1
server 192.168.14.131:8080 weight=1;
server 192.168.14.134:8080 weight=1;
}
server {
listen 80;
server_name localhost;
#即所有请求都到这里去找分配
location / {
#使用mytomcat分配规则,即刚自定义添加的upstream节点
proxy_pass http://mytomcat;
}
}
================================Content is below ================= ===
Project address: http://git.oschina.net/miki-long/nginx
Premise: I recently researched the usage of nginx and tried it on windows. Since nginx cache configuration is not supported under windows, this article mainly talks about nginx, reverse proxy and load balancing.
[1. Why use nginx]
To answer why to use nginx, let's talk about what nginx can do.
First of all, nginx can be a reverse proxy , so what is a reverse proxy? For example, I want to use the domain name of www.mickey.com to visit www.taobao.com locally. Then we can do it through nginx at this time.
Furthermore, nginx can achieve load balancing . What is load balancing? That is, my project is deployed on different servers, but through a unified domain name, nginx distributes requests, reducing the pressure on the server.
In the above two cases, the role of the nginx server is only as a distribution server . The real content can be placed on other servers. In this way, it can also play the role of a layer of security next door, and nginx acts as an isolation layer.
Secondly, nginx can also solve the problem of cross-domain.
[Second, nginx installation]
Download the corresponding version of nginx at http://nginx.org/
Use start nginx in the nginx directory or double-click nginx.exe to open nginx
[3, nginx configuration attribute description]
#Global Settings
main # run user user www-data; # Start the process, usually set to be equal to the number of CPUs worker_processes 1; # Global error log and PID file error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; # Working mode and connection limit events { use epoll; #epoll is a way of multiplexing IO (I/O Multiplexing), but it is only used for kernels above linux2.6, which can greatly improve the performance of nginx worker_connections 1024; #Maximum number of concurrent connections for a single background worker process # multi_accept on; } #Set up the http server and use its reverse proxy function to provide load balancing support http { #Set the mime type, the type is defined by the mime.type file include /etc/nginx/mime.types; default_type application/octet-stream; #Set the log format access_log /var/log/nginx/access.log; The #sendfile directive specifies whether nginx calls the sendfile function (zero copy mode) to output the file. For ordinary applications, #Must be set to on. If it is used for applications such as downloading and other applications with heavy disk IO load, it can be set to off to balance the processing speed of disk and network I/O and reduce the uptime of the system. sendfile on; #Set the two instructions tcp_nopush and tcp_nodelay to on to prevent network blocking tcp_nopush on; tcp_nodelay on; #Connection timeout keepalive_timeout 65; #Enable gzip compression gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; #Set request buffer client_header_buffer_size 1k; large_client_header_buffers 4 4k; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; #Set the server list for load balancing upstream mysvr { The #weigth parameter represents the weight, the higher the weight, the greater the probability of being assigned #Squid on this machine opens port 3128 server 192.168.8.1:3128 weight=5; server 192.168.8.2:80 weight=1; server 192.168.8.3:80 weight=6; } server { #Listen on port 80 listen 80; #Define access using www.xx.com server_name www.xx.com; #Set the access log of this virtual host access_log logs/www.xx.com.access.log main; #default request location / { root /root; #Define the default website root directory location of the server index index.php index.html index.htm; #Define the name of the home page index file fastcgi_pass www.xx.com; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include /etc/nginx/fastcgi_params; } # Define the error message page error_page 500 502 503 504 /50x.html; location = /50x.html { root /root; } #Static files, nginx handles it by itself location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/htdocs; # Expiration 30 days, static files are not updated very much, the expiration can be set larger, and if it is updated frequently, it can be set smaller. expires 30d; } #PHP script requests are all forwarded to FastCGI for processing. Use FastCGI default configuration. location ~ \.php$ { root /root; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; include fastcgi_params; } #Set the address for viewing Nginx status location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } #Disable access to .htxxx files location ~ /\.ht { deny all; } } #First virtual server server { #Listen on port 80 of 192.168.8.x listen 80; server_name 192.168.8.x; #Load balancing requests for aspx suffixes location ~ .*\.aspx$ { root /root; #Define the default website root directory location of the server index index.php index.html index.htm;#Define the name of the home page index file proxy_pass http://mysvr;#Request to the list of servers defined by mysvr #The following are some reverse proxy configurations that can be deleted. proxy_redirect off; #The back-end web server can obtain the user's real IP through X-Forwarded-For 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 10m; #The maximum number of bytes of a single file requested by the client client_body_buffer_size 128k; #The buffer proxy buffers the maximum number of bytes requested by the client, proxy_connect_timeout 90; #nginx connects to the backend server time out (the proxy connection time out) proxy_send_timeout 90; #Backend server data return time (Proxy send timeout) proxy_read_timeout 90; #After the connection is successful, the backend server response time (the proxy receives the timeout) proxy_buffer_size 4k; #Set the buffer size of the proxy server (nginx) to save the user header information proxy_buffers 4 32k; #proxy_buffers buffer, if the average web page is below 32k, set it like this proxy_busy_buffers_size 64k; #Buffer size under high load (proxy_buffers*2) proxy_temp_file_write_size 64k; #Set the size of the cache folder, larger than this value, it will be transmitted from the upstream server } } }
[four, nginx reverse proxy]
Two projects are started locally, and the source code is here .
Run under these two folders respectively
npm install node server.js
Type in the browser
Local ip: 4789
Local ip: 5789
Both pages can be accessed
Then we want to use
test.nginx.com visited page 5789
test.nginx.com/bug visits page 5789
Then we first need to configure hosts
The address of hosts under win is C:\Windows\System32\drivers\etc
We need to add the following configuration to the hosts file
172.18.144.23 test.nginx.com
Then add a server to the http module of nginx
server { listen 80; server_name test.nginx.com; location / { proxy_pass http://172.18.144.23:4789/; } location /buy { proxy_pass http://172.18.144.23:5789/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
Then restart nginx
Enter test.nginx.com in the browser
Enter test.nginx.com/bug in the browser
That's it for reverse proxies.
[V. nginx load balancing]
Configure http in nginx
First configure the load balancing service
Add the following configuration to the http module
upstream webservers { server 172.18.144.23:4789 weight=10; server 172.18.144.23:5789 weight=10; }
change server to
server { listen 80; server_name test.nginx.com; location / { proxy_pass http://webservers; } location /buy { proxy_pass http://172.18.144.23:5789/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
Enter test.nginx.com in the browser, refresh, we can see two pages, indicating that nginx has distributed our requests to different places.
Please indicate the source when reprinting http://www.cnblogs.com/Miss-mickey/p/6734831.html