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爲權重,根據權重來選擇優先轉發到哪臺服務器,以實現負載均衡。