Nginx安装与配置(Windows版)

1、下载Nginx

直接在官网下载稳定版本压缩包,解压到本地
在这里插入图片描述
在这里插入图片描述

2、操作Nginx

2.1、启动方式

一、进入解压目录下,直接双击nginx.exe,命令行窗口一闪而过
二、打开终端,进入解压目录下,输入命令 nginx.exe 或者 start nginx 回车

2.2、检查是否启动成功

浏览器访问http://localhost:80 如果出现以下界面则nginx启动成功
在这里插入图片描述
也可以通过命令行输入 tasklist /fi “imagename eq nginx.exe” 出现以下结果则nginx启动成功
在这里插入图片描述
如果启动失败,查看下是否是端口被占用,因为nginx默认监听的是80端口,命令行查看端口占用情况

 netstat -ano | findstr 0.0.0.0:80    
 或 
 netstat -ano | findstr "80"

如果80端口被占用了,则需要修改下nginx监听的端口号,找到nginx配置文件 conf/nginx.conf 找到以下位置,把80修改为其他未被占用的端口(如88),再重新启动查看是否能启动成功。
在这里插入图片描述
2.3、其他操作

nginx -s stop 快速退出
nginx -s quit 有序退出
nginx -s reload 重新加载配置文件
nginx -s reopen 重新打开日志文件

taskkill /f /t /im nginx.exe 终止进程

3、配置Nginx

3.1、配置文件基本结构

{
	main
	events {
		...
	}
	http {
		...
		server {
			...
			location {
				...
			}
		}
	}
}

在这里插入图片描述
3.2、访问静态资源
在这里插入图片描述
location 后面的 / 表示在浏览器中访问的路径 ,root 后面的html表示实际访问的是html路径
比如访问http://localhost:80/实际访问的是…/nginx/html/
http://localhost:80/hello.html ===> …/nginx/html/hello.html

在这里插入图片描述
访问/static ======> data/static/ data + /static
http://localhost:80/static/1.png ======⇒ …/nginx/data/static/1.png

3.3、服务代理
在这里插入图片描述
upstream tomcat_server设置要转发的多台服务器地址,
通过location的proxy_pass把请求转发到其他服务器,其中weight为权重,根据权重来选择优先转发到哪台服务器,以实现负载均衡。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章