当前位置:文档之家› NGinx分布式部署测试实例图解

NGinx分布式部署测试实例图解

+Mvc+NGinx+IIS分布式部署和负载平衡实例图解目的:在IIS中创建四个相同的网站,浏览器访问同一个地址,通过NGinx转到不同的IIS 网站。

测试条件:Windows 7 X64旗舰版+Visual Studio 2017专业版1、创建 +Mvc WebApplication在VS运行后的页面效果如下:2、发布网站3、IIS中部署四个网站创建三个应用程序池,端口分别为:8001、8002、8003,8004分别对应四个网站。

为区分三个网站,把Index.cshtml文件中的“Nginx测试程序”这行字分别改为:Nginx测试程序--------1Nginx测试程序--------2Nginx测试程序--------3Nginx测试程序--------4如下图:部署四个网站后,打开页面效果如下:到此,说明四个网站部署后单独访问都没有问题。

下面开始部署NGInx的负载平衡。

4、NGInx的安装4.1、下载nginx-1.15.1下载后解压到D:\nginx文件夹,解压后的文件夹如下:4.2、修改配置文件nginx.confnginx.conf文件位于D:\nginx\conf目录下,nginx.conf默认内容如下。

用记事本打开文件,nginx.conf文件的内容如下:#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 {worker_connections 1024;}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;# }#}}(1)修改侦听端口刚才部署的网站在8001-8004端口,将侦听端口改为8888,(即:最终通过8888端口访问网站)修改后如下:listen 8888;(2)总共部署的网站数量刚才部署了四个网站,修改后的配置如下:worker_processes 3;(3)连接数(采用默认值1024)worker_connections 1024;5、启动NGinx打开CMD命令行窗口,Cd D:\nginx\启动:start nginx关于nginx.pid文件刚解压nginx的时候,logs目录下并没有nginx.pid文件,但当执行start nginx 命令后,即自动产生这个文件,用记事本打开start nginx文件pid文件就是一个纯文本文件,里面记录的是进程的pid号如何查看PID重启动:当配置信息修改,需要重新载入这些配置时使用此命令。

nginx.exe -s reload6、关于NgInx的启动与停止Windows下Nginx的启动、停止等命令在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动、停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍。

1、启动:C:\server\nginx-1.0.2>start nginx或C:\server\nginx-1.0.2>nginx.exe注:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。

启动轶事,日志文件会写入一行文本:2018/07/24 00:11:28 [notice] 7452#8032: signal process started启动后如何检查是否启动成功呢?输入命令tasklist /fi "imagename eq nginx.exe" 看到以下信息说明启动成功了因为配置worker_processes 2;加一个主线程,所以是三个线程。

(不知道是否正确)2、停止:C:\server\nginx-1.0.2>nginx.exe -s stop或C:\server\nginx-1.0.2>nginx.exe -s quit注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。

3、重新载入Nginx:C:\server\nginx-1.0.2>nginx.exe -s reload当配置信息修改,需要重新载入这些配置时使用此命令。

4、重新打开日志文件:C:\server\nginx-1.0.2>nginx.exe -s reopen5、查看Nginx版本:C:\server\nginx-1.0.2>nginx –v7、nginx配置文件nginx.conf超详细讲解https:///liang-wei/p/5849771.html8、最终修改后的nginx.conf文件9、关于nginx.pid文件在运行start nginx命令时,会自动在D:\nginx\logs目录创建一个文件:nginx.pid内容如下:记录当前的PID。

10、最后效果。

相关主题