What is a reverse proxy

When we talk about reverse proxies, let’s talk about “forward proxies,” for example, we want to see the outside world. Sorry, there’s a wall. We have to look for scientific web tools, and these tools are forward proxies, so if we go to YouTube, YouTube doesn’t know about us, they just know about proxies. So the object of the forward proxy is the client.

It is much easier for us to visit domestic websites, such as Baidu. I only know Baidu, I don’t know how many servers it has in the background to handle our request. Baidu is acting as the reverse agent. Its service object is the server. Nginx is an excellent reverse proxy server.

** The forward proxy service objects are clients, and the reverse proxy service objects are servers. **

Nginx common commands

  • Start the nginx.
  • Stop nginx -s stop or nginx -s quit.
  • /sbin/nginx -s reload or service nginx reload.
  • Reloading the specified configuration file. The nginx – c/usr/local/nginx/conf/nginx. Conf.
  • View the nginx version nginx -v.
  • Check whether the configuration file is correct nginx-t.
  • Displays help information nginx-h.

Common load balancing algorithms.

  • Polling + weight
  • Ip_hash // Each request is allocated based on the hash result of the access IP, so that each visitor has fixed access to one back-end server
  • Least_conn // The next request will be dispatched to the server with the fewest active connections
  • Least_time // Requests are allocated to backend with the fastest response and fewest active connections
  • Url_hash // tripartite plugin, each request is allocated according to the URL result of the access IP
  • Fair // Tripartite plugin for intelligent load balancing based on page size and load time, that is, requests are allocated based on the response time of the back-end server, with priority given to those with short response times. The upstream_fair module for Nginx must be downloaded.

The key configuration

worker_processes 8; The recommended value is equal to the total number of cores in the CPU. error_log logs/error.log; / / global error log definition type, [the debug | info | notice | warn | error | crit] # error_log logs/error. The log notice; #error_log logs/error.log info; pid logs/nginx.pid; / / process file # working mode and the upper limit of the number of connections events {reference event model, the use of [kqueue | rtsig | epoll | / dev/poll | select | poll]; The Epoll model is a high-performance network I/O model in the Linux 2.6 or higher kernel. If running on FreeBSD, the Kqueue model is used. use epoll; Maximum number of connections for a single process (Maximum number of connections = Number of connections x Number of processes) worker_connections 65535; } http { include mime.types; Default_type application/octet-stream; The default file type is charset UTF-8. # client_header_BUFFer_size 32K; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; Sendfile specifies whether nginx calls sendFile to output files. Set it to ON for common applications. Set it to off for heavy load applications such as downloading disk I/O to balance disk and network I/O processing speed and reduce system load. #tcp_nopush # Optimize TCP data transfer on Linux /Unix systems, autoindex on only when sendFile is turned on; # Enable directory list access, suitable for download server, default disabled. keepalive_timeout 65; Gzip set gzip on; gzip_min_length 1k; gzip_buffers 4 16k; Gzip_http_version 1.0; gzip_comp_level 6; gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml; gzip_vary on; # http_proxy set client_max_body_size to 10m; client_body_buffer_size 128k; proxy_connect_timeout 75; proxy_send_timeout 75; proxy_read_timeout 75; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_temp_path /usr/local/nginx/proxy_temp 1 2; Upstream {server 192.168.3.101:80 weight=10 down max_conns=100; Backup server 192.168.3.105:80 weight = 2; } * weight: Defaults to 1. The greater the weight, the greater the weight of the load. * Down: indicates that the current server is temporarily not involved in the load * backup: indicates that all other non-backup machines are down or busy, request the backup machine. * max_fails, fail_timeout # max_fails: How many times does the host fail? If it is considered to have failed, it should be kicked out. If the company has few resources, it should be set to 2 or 3 times. Max_fails =2 FAIL_timeout =20s Indicates that if two requests for an application fail within 20 seconds, the application is considered to be down. After 20 seconds, new requests are not sent to the down application during this period. Instead, new requests are directly sent to the normal application. If the connection fails again, wait 20 seconds... This cycle continues until recovery. Server {listen 80; Server_name tt.kk.com; root /web/kk; charset utf-8; access_log logs/host.access.log main; # proxy location / {root /web/kk; index index.jsp index.html index.htm; proxy_pass http://https; # health check module location /status {check_status; }} ### configure static and static separation. location / { proxy_pass httpds; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|html|htm|css|js)$ { root /var/data/web/; }Copy the code

Added: Boot nginx

chkconfig --list
chkconfig --add nginx
chkconfig nginx on
Copy the code

Nginx high concurrency principle: Multiple processes +epool to achieve high concurrency

  1. After Nginx starts, there will be a master process and multiple independent worker processes.
  2. Each sub-process has only one thread, using the IO multiplexing model epoll, to achieve high concurrency

Nginx is different from Ribbon

Nginx is server-side load balancing: proxy is used between client and server. The Ribbon is client load balancing: the Ribbon loads itself.

Nginx error port

502: wrong gateway; 503: Server is overloaded