Nginx

概述

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器。

反向代理

什么是正向代理?

访问某个网址如谷歌,需要进行配置,并通过代理服务器才能访问,代理服务器所起到的作用是正向代理。如FQ这个操作就是正向代理

正向代理是给用户做代理,也就是作用于客户端。

什么是反向代理?

反向代理是,用户对代理服务器和目标服务器是无感知的,用户访问代理服务器,代理服务器转发请求到目标服务器,并把返回结果返回给用户。这里的代理服务器起到了反向代理的作用。

反向代理是给服务器做代理,作用于服务端。

负载均衡

将多个请求经由代理服务器转发到多台服务器上。

动静分离

将静态资源和动态资源进行分离,请求静态资源(js、html、img等)时,请求转发到指定的负责静态资源的服务器。

安装

brew install nginx

## 打开http://localhost:8080,看到这个页面说明启动成功
brew services start nginx

命令

## 查看nginx版本及安装的本地位置
nginx -V 

## 查看版本号
nginx -v

## 关闭
./nginx -s stop  

## 打开
./nginx


## 重新加载,使得配置文件生效
./nginx -s reload


配置文件

配置文件分为3大块:全局块、event块、http块

全局块


#user  nobody;
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块

events {
    worker_connections  1024;
}

http块

http块又包含Http全局块、server块。

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       8080;
        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;
    #    }
    #}
    include servers/*;
}

操作实例

单一请求转发

修改nginx.conf

在location中新增proxy_pass参数,当访问127.0.0.1:8080时,请求会转发到http://127.0.0.1:9980,客户端看到的访问地址不变,依然是127.0.0.1:8080

server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass http://127.0.0.1:9980;
            index  index.html index.htm;
        }
}

请求过滤转发

若路径包含edu,则请求转发到A服务器上,若路径包含vod,则请求转发到B服务器。

server {
        listen       8000;
        server_name  localhost;

        location ~ /edu/ {
            proxy_pass http://127.0.0.1:9980;
        }

        location ~ /vod/ {
            proxy_pass http://127.0.0.1:9981;
        }
    }

若访问localhost:8000/vod/则请求http://127.0.0.1:9981/vod/

posted @ 2016-12-21 11:55  清泉白石  阅读(238)  评论(0编辑  收藏  举报