web-nginx

253 阅读2分钟

作用

转发请求

为什么要转发请求?

1.问题描述

集群

2.原因分析

3.解决方法
使用nginx进行负载均衡


1.问题描述

专线

对接银行,需要专线
省内拉专线和跨省拉专线的价格不同
为了省钱
省内拉专线
省外使用外网
中间通过nginx进行转发请求

省份A:服务器——》省份B:nginx——》银行

2.原因分析

3.解决方法 配置nginx套接字接受请求和转发请求模块

适合处理静态资源的请求

为什么适合处理静态资源的请求呢?
因为适合高并发

为什么适合高并发呢?

用途

1.作为静态服务器

具体细节?
静态资源文件部署在哪里?

2.作为中转服务器

负载均衡

2个重要的配置

1.配置目标服务器

配置项
proxy_pass

配置方法
1.单机
proxy_pass ip

2.集群
proxy_pass ip集群名字

2.重定向

配置项?
proxy_redirect

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

作用?

怎么配置?

与tomcat重定向的关系?

2个重要的模块

1.http请求模块

2.套接字请求模块

#套接字请求
#proxy socket connection
stream{
    upstream bank_server{
        #bank server
        server ip:8091; 
    }
    server{
        #local server listen socket connection port
        listen 8091;
        proxy_pass bank_server;
    }
}

完整的配置文件

#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请求
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;
    #    }
    #}

}


#套接字请求
#proxy socket connection
stream{
    upstream bank_server{
        #bank server
        server ip:8091; 
    }
    server{
        #local server listen socket connection port
        listen 8091;
        proxy_pass bank_server;
    }
}

2大核心技术

1.upstream机制

基于upstream机制,可以实现以下核心模块:
1.http核心模块
2.socket核心模块

2.事件驱动框架

双机高可用

2种解决方案

1.一主一备

一台在跑

一台空闲
出了问题,才接管

2.两主

两台同时在跑

一台出现故障
另外一台全部接管

参考张宴《nginx》