role

Forward the request

Why forward the request?

1. Problem description

The cluster

2. Cause analysis

3. Solution Use Nginx for load balancing


1. Problem description

Special line

In order to save money, a private line is required to connect to a bank. The price of a private line is different between a private line within a province and an inter-provincial private line

Province A: server — province B: nginx — bank

2. Cause analysis

3. Solution Configure the nginx socket to accept and forward requests

Suitable for handling requests for static resources

Why is it appropriate to handle requests for static resources? Because it is suitable for high concurrency

Why is it suitable for high concurrency?

use

1. Serve as a static server

The details? Where are static resource files deployed?

2. Serve as a transfer server

Load balancing

Two important configurations

1. Configure the target server

Configuration items proxy_pass

1. Single-node proxy_pass IP address

2. Cluster proxy_pass IP Cluster name

2. The redirection

Configuration items? proxy_redirect

Configuration method? proxy_redirect ~^http://([^:]+)(:\d+)? (.*)$ https://$1$3; http://www.oschina.net/question/1378083_234719

Role?

How to configure it?

Relationship to Tomcat redirection?

Two important modules

1. HTTP request module

2. Socket request module

Socket request
#proxy socket connection
stream{
    upstream bank_server{
        #bank server
        server ip:8091; 
    }
    server{
        #local server listen socket connection portlisten 8091; proxy_pass bank_server; }}Copy the code

Complete configuration file

#user nobody;
#enable right
user  root;
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
    worker_connections  1024;
}



# HTTP requests
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"';

    #access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout  65;

    #gzip on;

    server {
        listen      80;
        server_name  localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
            root  html;
            index  index.html index.htm;
        }

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root  html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        # proxy_pass http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        # root html;
        # fastcgi_pass 127.0.0.1:9000;
        # fastcgi_index index.php;
        # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
        # include fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        # deny all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    # listen 8000;
    # listen somename:8080;
    # server_name somename alias another.alias;

    # location / {
    # root html;
    # index index.html index.htm;
    #}
    #}


    # HTTPS server
    #
    #server {
    # listen 443 ssl;
    # server_name localhost;

    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.key;

    # ssl_session_cache shared:SSL:1m;
    # ssl_session_timeout 5m;

    # ssl_ciphers HIGH:! aNULL:! MD5;
    # ssl_prefer_server_ciphers on;

    # location / {
    # root html;
    # index index.html index.htm;
    #}
    #}

}


Socket request
#proxy socket connection
stream{
    upstream bank_server{
        #bank server
        server ip:8091; 
    }
    server{
        #local server listen socket connection portlisten 8091; proxy_pass bank_server; }}Copy the code

Two core technologies

1. The upstream mechanism

Upstream implements the following core modules: HTTP core module and socket core module

2. Event-driven frameworks

Dual-node high availability

Two solutions

1. One master and one standby

A running

When one is idle and has problems, it takes over

2. The two main

Both are running at the same time

When one fails, the other one takes over

Reference zhang Yan’s nginx