下載
地址: nginx(http://nginx.org/en/download.html)
下載后,直接解壓喧兄,解壓后如下圖:
啟動(dòng)nginx
- 雙擊nginx.exe,在瀏覽器輸入http://localhost:80回車狼荞,出現(xiàn)以下畫面即成功
image.png -
打開cmd,進(jìn)入到nginx目錄闽寡,輸入命令start nginx 或nginx.exe 代兵,回車即可
image.png
關(guān)閉nginx
使用cmd命令窗口,輸入如下命令taskkill /f /t /im nginx.exe 或 nginx -s stop 或 nginx -s quit
修改端口號(hào)
默認(rèn)的端口號(hào)是80爷狈,可以根據(jù)端口使用情況進(jìn)行修改
-
查看端口占用情況 植影,在cmd輸入如下命令 netstat -ano | findstr "80"
image.png -
nginx的配置文件是conf/nginx.conf,按照下圖修改端口即可
image.png 修改完配置文件后,重啟nginx,可輸入命令 nginx -s reload
部署前端vue項(xiàng)目
-
將打包后的dist文件復(fù)制到html文件下
image.png 修改conf/nginx.conf文檔
(root默認(rèn)是html涎永,配置為html/dist思币,自動(dòng)找到dist/index.html打開)
- 重啟nginx,即可
使用nginx代理做負(fù)載均衡
upstream webName{ #webName 取得服務(wù)器名字
server 192.168.11.186:8888; #不要加http
}
server {
listen 8081;
server_name localhost;
location / {
proxy_pass http://webName;
}
}
此時(shí),訪問localhost:8081就會(huì)自動(dòng)跳轉(zhuǎn)到http://192.168.11.186:8888項(xiàng)目中去
也可以配置多個(gè)目標(biāo)服務(wù)器羡微,當(dāng)當(dāng)一臺(tái)機(jī)器出現(xiàn)故障谷饿,nginx會(huì)自動(dòng)轉(zhuǎn)向另一臺(tái),weight代表權(quán)重妈倔,值越大被訪問的幾率更高
upstream webName{ #webName 取得服務(wù)器名字
server 192.168.11.186:8801 weight=3;
server 192.168.11.186:8802 weight=2;
server 192.168.11.186:8803 weight=1;
}
server {
listen 8081;
server_name localhost;
location / {
proxy_pass http://webName;
}
}
備注
若 命令是通過powershell打開的博投,有些命令可能會(huì)出現(xiàn)以下報(bào)錯(cuò)的提示,只需切回cmd即可解決
來源: https://blog.csdn.net/sinat_34626741/article/details/115519348