1.下载Nginx
下载地址:http://nginx.org/en/download.html
本文所选版本:nginx-1.4.7
2.下载后需要解压,解压后的nginx目录如下:
3.新建测试html页,并命名为:“default.htm”
<html>
<head>
<title>nginx学习</title>
</head>
<body>
Welcome to localhost:8001 port website !
<body>
</html>
4.本地IIS搭建4个网站。如下所示。每个网站的端口号相对应,比如nginx-6001,那么它的端口号就是6001,本地访问:http://localhost:6001
5.118服务器搭建1个网站。局域网服务器端口号为:6005,局域网访问:http://192.168.0.118:6005或者localhost:6005
6.228服务器搭建1个网站。局域网服务器端口号为:6006,局域网访问:http://192.168.0.228:6006或者localhost:6006
7.每个网站的目录下存放default.htm文件。并修改default.htm文件中的IP和端口号。请参考上面第3点
8.找到nginx目录下的conf中的nginx.conf文件,先复制一份副本,避免改错原版。再用记事本打开方式进行编辑。
9.以下是nginx.conf文件,带#号为前缀的表示注释,#后面的单行字符将均为注释内容,红色部分表示为新增的功能。
【分发配置】
upstream name_erp 中upstream表示上游模块,name_erp表示模块配置的名称,括号里包含了分发给目标服务器(这里即本机)访问的ip和端口号,weight代表权重。你可以理解为负载百分比。
name_api表示另外一个模块,模块是api网站的分发。该模块中包含了局域网其他主机电脑的ip和端口号
【监听配置】
listen 表示监听的端口号,这里是监听8889端口号
listen下面的server_name 表示监听的IP,这里是监听localhost.
proxy_pass 表示指向反向代理的配置,这里指向了name_erp配置,并增加了前缀http,如果是https访问则需要修改
监听配置意在表示,将对本机localhost:8889请求时会进行监听。届时会分发到name_erp中的目标ip和端口号网站中。
#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; upstream name_erp{ #+add server localhost:6001 weight=2;#+add#weight代表权重,数据越大,分担越多 server localhost:6002 weight=1;#+add server localhost:6003 weight=1;#+add server localhost:6004 weight=1;#+add }#+add upstream name_api{ #+add server 192.168.63.118:6005 weight=1;#+add server 192.168.60.228:6006 weight=1;#+add }#+add server { listen 8888;#+up-old=80 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://name_erp;#+add 志向指定的服务器集群(一定要加,否则页面一直指向欢迎页,不知道指向那个页面) } #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 # 另一个虚拟主机混合使用基于IP、名称和端口的配置 #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; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} #HTTPS 服务器 server { listen 8889; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://name_api;#+add } } }
在最后面再增加一个监听Https服务器的配置。内容与上方大致一致。
这里指定的proxy_pass指向了name_api。
监听端口为8888,监听IP为localhost.
监听配置意在表示,将对本机localhost:8888请求时会进行监听。届时会分发到name_api中的目标ip和端口号网站中。
注意此处的监听端口号不能与上方监听的端口号一致,避免冲突。
10.安装路径:这里把nginx相关文件放在英文目录下,中文目录下不会生效。
11.运行cmd,启动Nginx,并输入以下命令:
定位到nginx目录:
cd 存放nginx的英文根目录路径
启动nginx:
start nginx
12.nginx命令大全:
杀掉所有Nginx进程并启动
taskkill /IM nginx.exe /F
start nginx