q986 发表于 2017-6-28 12:23:41

windows下Nginx配置与测试

  1.获取
  下载地址:http://nginx.org/en/download.html
  参考网址:http://nginx.org/en/docs/windows.html
  2.准备工作,IIS创建俩个测试网站,参考截图:
  
  两个网站的地址分别是(我本地的地址是192.168.31.233),两个网站的名称分别是端口的名称,故两个网站的地址分别是 192.168.31.233:8087和192.168.31.233:8088
  3.nginx配置 (解压之后 的conf文件夹中的 nginx.conf文件)
  【解压路径放置到 非中文路径,建议不要有空格,之前测试rabbitmq的时候,也遇到很多坑其实都是因为空格和中文路径】





#usernobody;
#指定nginx进程数量
worker_processes1;
#全局错误日志以及 PID文件
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pid      logs/nginx.pid;

events {
# 连接数上限
worker_connections1024;
}

http {
#设定http服务器,利用它的反向代理功能提供均衡负载支持
include       mime.types;
default_typeapplication/octet-stream;
#设定日志格式
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#使用哪种格式的日志
#access_loglogs/access.logmain;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
sendfile      on;
#tcp_nopush   on;
#连接超时时间
#keepalive_timeout0;
keepalive_timeout65;
#开启gzip压缩
#gzipon;

#设定负载均衡的服务器列表 支持多组的负载均衡,可以配置多个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.233down;
#server 192.168.31.233backup;
server 192.168.31.233:8087weight=1;
server 192.168.31.233:8088weight=2;
}
#配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址
server {
#1.侦听80端口
listen       8086;
#对于server_name,如果需要将多个域名的请求进行反向代理,可以配置多个server_name来满足要求
server_namelocalhost;
#charset koi8-r;
#access_loglogs/host.access.logmain;
location / {
# 默认主页目录在nginx安装目录的html子目录。
root   html;
indexindex.html index.htm;
proxy_pass http://mysvr; #跟载均衡服务器的upstream对应
      }
#error_page404            /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_indexindex.php;
#    fastcgi_paramSCRIPT_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 {
#    denyall;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_namesomenamealiasanother.alias;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_namelocalhost;
#    ssl_certificate      cert.pem;
#    ssl_certificate_keycert.key;
#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout5m;
#    ssl_ciphersHIGH:!aNULL:!MD5;
#    ssl_prefer_server_cipherson;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}
}
View Code  4.启动nginx
  cmd cd到解压目录, 执行start nginx运行 nginx

  然后是通过 tasklist /fi "imagename eq nginx.exe"判断是否运行成功。
  这时候就可以访问 server中的指定的 server_name和监听的 端口地址,比如我这边 监听端口是 8086,所以我的访问地址是 http://localhost:8086/
  这时候可以发现不停的刷新页面的时候,指向的地址在8088和8087之间的两个网站在不停的切换。
  注意:因为是使用 IIS,IIS默认端口是80,我之所以换成 8086也是为了避免这个问题。
页: [1]
查看完整版本: windows下Nginx配置与测试