第一步:安装nginx
下载windows版nginx (http://nginx.org/en/download.html) ,安装在E:\software\nginx-1.15.9
将nginx配置成为windows服务
需要借助"Windows Service Wrapper"小工具,项目地址: https://github.com/kohsuke/winsw
下载地址: http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/1.18/winsw-1.18-bin.exe
下载该工具后,将其放在 Nginx安装目录下,并重命名为nginx-service.exe,创建配置文件nginx-service.xml(名字要和工具名一样),
创建nginx-service.exe.config(为支持NET 4.0 runtime,默认只支持NET 2.0 runtime)
文件结构:
nginx-service.xml 配置:
<service>
<id>nginx</id>
<name>Nginx Service</name>
<description>High Performance Nginx Service</description>
<logpath>E:\software\nginx-1.15.9\logs</logpath>
<log mode="roll-by-size">
<sizeThreshold>10240</sizeThreshold>
<keepFiles>8</keepFiles>
</log>
<executable>E:\software\nginx-1.15.9\nginx.exe</executable>
<startarguments>-p E:\software\nginx-1.15.9</startarguments>
<stopexecutable>E:\software\nginx-1.15.9\nginx.exe</stopexecutable>
<stoparguments>-p E:\software\nginx-1.15.9 -s stop</stoparguments>
</service>
nginx-service.exe.config 配置:
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v4.0" />
</startup>
<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>
</configuration>
在cmd中运行如下命令安装windows服务(E:\software\nginx-1.15.9\nginx-service.exe install)
之后就可以在Windows服务面板中启动服务了
浏览器访问看看
当使用nginx在同一个域名下配置多个项目:这里只展示1
- nginx按不同的目录分发给不同的项目
- 启用二级域名,不同的项目分配不同的二级域名
注意:这三个项目属于不同的域名,项目之间通过http访问会存在跨域问题。
server {
listen 80;
server_name example.com;
location ^~ /admin {
proxy_pass http://localhost:9092;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /customer {
proxy_pass http://localhost:9091;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
proxy_pass http://localhost:9090;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
nginx的状态监控
通过查看nginx的并发连接,我们可以更清楚的知道网站的负载情况。在Web界面比较精确的查看如下:
在nginx.conf中的server里面加入
location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; 访问IP deny all; }
配置完后重新启动Nginx后我们可以通过浏览器访问 http://loclahost/nginx_status查看,如下图:
解析:Active connconnections //当前Nginx正处理的活动连接数。
server accepts handledrequests //总共处理了42个连接,成功创建42次握手,总共处理了146个请求。
Reading //nginx读取到客户端的Header信息数。
Writing //nginx返回给客户端的Header信息数。
Waiting //开启keep-alive的情况下,这个值等于 active-(reading +writing) ,意思就是Nginx已经处理完正在等候下一次请求指令的驻留连接No2
root和alias都可以定义在location模块中,都是用来指定请求资源的真实路径。。。
root :真实的路径是root指定的值加上location指定的值 。
alias :指定的路径是location的别名,不管location的值怎么写,资源的 真实路径都是 alias 指定的路径
其他区别:
1、 alias 只能作用在location中,而root可以存在server、http和location中。
2、alias 后面必须要用 “/” 结束,否则会找不到文件,而 root 则对 ”/” 可有可无。