Nginx 本地测试(反向代理 Http)

Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。

在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

 

 下载地址:http://nginx.org/en/download.html

 

简单来讲:正向代理代理的是客户端,反向代理对象是服务器

反向代理示意图:

 

 

 

 

正向代理示意图:

 

 

 

Windows 版本安装

  我们将上一步下载的 nginx-1.14.0.zip 文件解压到当前目录。

  

  解压目录如下:

  

  下面对这个目录下的主要文件夹进行介绍:

  1、conf 目录:存放 Nginx 的主要配置文件,很多功能实现都是通过配置该目录下的 nginx.conf 文件,后面我们会详细介绍。

  2、docs 目录:存放 Nginx 服务器的主要文档资料,包括 Nginx 服务器的 LICENSE、OpenSSL 的 LICENSE 、PCRE 的 LICENSE 以及 zlib 的 LICENSE ,还包括本版本的 Nginx服务器升级的版本变更说明,以及 README 文档。

  3、html 目录:存放了两个后缀名为 .html 的静态网页文件,这两个文件与 Nginx 服务器的运行相关。

  4、logs 目录:存放 Nginx 服务器运行的日志文件。

  5、nginx.exe:启动 Nginx 服务器的exe文件,如果 conf 目录下的 nginx.conf 文件配置正确的话,通过该文件即可启动 Nginx 服务器。

  一、启动 nginx

  双击解压之后目录中的 nginx.exe 文件,出现一闪而过的画面,则启动成功。

  然后在浏览器中输入 http://localhost 或者 http://localhost:80 出现如下界面即启动成功。

  

  ps:该页面即是上面解压目录中 html 目录下的 index.html 文件。

   二、关闭 nginx

  进入到解压之后的目录,输入如下命令: 

  nginx.exe -s stop

  

  或者也可以打开任务管理器,找到 nginx 的进程,直接右键结束。

 

本地创建两个站点模拟测试:

192.168.124.67:55268   与   192.168.124.67:55268  

   修改 conf 文件夹下 的 nginx.conf

   

 

#user  nobody;
#指定nginx进程数量
worker_processes  1;

#全局错误日志以及 PID文件
#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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用, 
    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #开启gzip压缩
    #gzip  on;
    
    
    #设定负载均衡的服务器列表 支持多组的负载均衡,可以配置多个upstream  来服务于不同的Server.
    #nginx 的 upstream 支持 几 种方式的分配 
    #1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 
    #2)、weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 跟上面样,指定了权重。
    #3)、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 
    #4)、fair       
    #5)、url_hash #Urlhash
    upstream mysvr {
      #weigth参数表示权值,权值越高被分配到的几率越大   
      #1.down 表示单前的server暂时不参与负载
      #2.weight 默认为1.weight越大,负载的权重就越大。     
      #3.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。  
      #server 192.168.31.233  down;
      #server 192.168.31.233  backup;
      server 192.168.124.67:55268  weight=1;
      server 192.168.124.67:5745  weight=2;
	  
    }
    
    #配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址
    server {
        #1.侦听8086端口 
        listen       8086;
        
        #对于server_name,如果需要将多个域名的请求进行反向代理,可以配置多个server_name来满足要求
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # 默认主页目录在nginx安装目录的html子目录。
            root   html;
            index  Login.aspx;
            proxy_pass http://mysvr; #跟载均衡服务器的upstream对应
        }

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

}

  

当访问 localhost:8086 时返回两个的站点信息,测试完毕。

 

#user  nobody;#指定nginx进程数量worker_processes  1;
#全局错误日志以及 PID文件#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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,     sendfile        on;    #tcp_nopush     on;
    #连接超时时间    #keepalive_timeout  0;    keepalive_timeout  65;
    #开启gzip压缩    #gzip  on;            #设定负载均衡的服务器列表 支持多组的负载均衡,可以配置多个upstream  来服务于不同的Server.    #nginx 的 upstream 支持 几 种方式的分配     #1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。     #2)、weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 跟上面样,指定了权重。    #3)、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。     #4)、fair           #5)、url_hash #Urlhash    upstream mysvr {      #weigth参数表示权值,权值越高被分配到的几率越大         #1.down 表示单前的server暂时不参与负载      #2.weight 默认为1.weight越大,负载的权重就越大。           #3.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。        #server 192.168.31.233  down;      #server 192.168.31.233  backup;      server 192.168.124.67:55268  weight=1;      server 192.168.124.67:5745  weight=2;      }        #配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址    server {        #1.侦听80端口         listen       8086;                #对于server_name,如果需要将多个域名的请求进行反向代理,可以配置多个server_name来满足要求        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {            # 默认主页目录在nginx安装目录的html子目录。            root   html;            index  Login.aspx;            proxy_pass http://mysvr; #跟载均衡服务器的upstream对应        }
        #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;        #}    } 
}

posted @ 2021-03-02 15:39  越过那个限制  阅读(1099)  评论(0编辑  收藏  举报