下載
地址: nginx(http://nginx.org/en/download.html)

image.png
下載后,直接解壓,解壓后如下圖:

image.png
啟動nginx
- 雙擊nginx.exe,在瀏覽器輸入http://localhost:80回車,出現(xiàn)以下畫面即成功
image.png -
打開cmd,進入到nginx目錄,輸入命令start nginx 或nginx.exe ,回車即可
image.png
關閉nginx
使用cmd命令窗口,輸入如下命令taskkill /f /t /im nginx.exe 或 nginx -s stop 或 nginx -s quit

image.png
修改端口號
默認的端口號是80,可以根據(jù)端口使用情況進行修改
-
查看端口占用情況 ,在cmd輸入如下命令 netstat -ano | findstr "80"
image.png -
nginx的配置文件是conf/nginx.conf,按照下圖修改端口即可
image.png 修改完配置文件后,重啟nginx,可輸入命令 nginx -s reload
部署前端vue項目
-
將打包后的dist文件復制到html文件下
image.png 修改conf/nginx.conf文檔
(root默認是html,配置為html/dist,自動找到dist/index.html打開)

image.png
- 重啟nginx,即可
使用nginx代理做負載均衡
upstream webName{ #webName 取得服務器名字
server 192.168.11.186:8888; #不要加http
}
server {
listen 8081;
server_name localhost;
location / {
proxy_pass http://webName;
}
}
此時,訪問localhost:8081就會自動跳轉(zhuǎn)到http://192.168.11.186:8888項目中去
也可以配置多個目標服務器,當當一臺機器出現(xiàn)故障,nginx會自動轉(zhuǎn)向另一臺,weight代表權(quán)重,值越大被訪問的幾率更高
upstream webName{ #webName 取得服務器名字
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打開的,有些命令可能會出現(xiàn)以下報錯的提示,只需切回cmd即可解決

image.png
來源: https://blog.csdn.net/sinat_34626741/article/details/115519348




