1. 筆記資料來源
筆記來源自 狂神的 nginx講說 Nginx入門 和 程式羊的 保姆級教程
2. Nginx簡介
1. 什麼是Nginx?
Nginx (engine x)是一個高性能的HTP和反向代理web伺服器,同時也提供了IMAP/POP3/SMTP服務。Nginx是由伊戈爾·賽索耶夫為俄羅斯通路量第二的Rambleru站點(俄文:PaM6nep)開發的,第一個公開版本0.1.0釋出于2004年10月4日。2011年6月1日,nginx 1.0.4釋出。
其特點是占有記憶體少,并發能力強,事實上ngino的并發能力在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度、京東、新浪、網易、騰訊、淘寶等。在全球活躍的網站中有12.18%的使用比率,大約為2220萬個網站。
Nginx是一個安裝非常的簡單、配置檔案非常簡潔(還能夠支援peri文法)、Bug非常少的服務。Nginx啟動特别容易,并且幾乎可以做到7*24不間斷運作,即使運作數個月也不需要重新啟動。你還能夠不間斷服務的情況下進行軟體版本的更新。
Nginx代碼完全用C語言從頭寫成。官方資料測試表明能夠支援高達50,000個并發連接配接數的響應。
2. Nginx的作用
Http代理,反向代理:作為web伺服器最常用的作用之一,尤其是反向代理
- 正向代理
- 反向代理
Nginx提供的負載均衡政策有兩種,内置政策和擴充政策,
内置政策為輪詢、權重輪詢、 ip hash 、
擴充政策是由使用者自己定義了
- 輪詢
- 權重輪詢
動靜分離,在我們的軟體開發中,有些請求是需要背景處理的,有些請求是不需要經過背景處理的(如: css、 html.jpg. js等等檔案),這些不需要經過背景處理的檔案稱為靜态檔案。讓動态網站裡的動态網頁根據一定規則把不變的資源和經常變的資源區分開來,動靜資源做好了拆分以後,我們就可以根據靜态資源的特點将其做緩存操作。提高資源響應的速度。
3. Nginx安裝
1. Windows下安裝
- 下載下傳Nginx
下載下傳位址 最好下載下傳穩定版本的,可以下載下傳 nginx/Window-1.16.1,下載下傳成功之後解壓就行了
連結: https://pan.baidu.com/s/1G7Qz1fy9jSSttiacpMIL_g 提取碼: x4qg
2. 啟動Nginx
- 直接輕按兩下nginx.exe,啟動成功會一閃而過
- 使用cmd 啟動nginx.exe
錯誤集錦
nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
錯誤原因: 80端口被占用
解決辦法
- Win + R 或者打開 cmd ,輸入
netstat -ano|findstr :80
- 接着使用
查詢是那個服務占用了tasklist | findstr 4
- 如果端口号是
,是4
程序占用System
端口的話,需要進行關閉,否則直接使用80
關閉即可taskkill /pid 4 /f
- 使用
檢視程序号netsh http show servicestate
- 打開任務管理器,找到PID是
的程序關閉掉即可,或者是關閉系統的4632
IIS Admin 服務
- 再次進入安裝目錄
運作nginx,沒有任何提示資訊通路nginx.exe
或者localhost:80
出現下邊的界面即可,因為 http預設端口是localhost
,是以不加也可以80
2. Linux下安裝
下載下傳位址 下載下傳成功之後上傳到linux中 scp niginx.tar.gz [email protected]:/opt
- 檢視是否安裝nginx
,如果沒提示資訊就是沒有安裝whereis nginx
- 使用
解壓tar -zxvf nginx.tar.gz
-
檢視目錄,與window應該是一緻的cd nginx
- 在此目錄下執行
./configure
- 執行
以及make
make install
- 檢視nginx配置目錄
whereis nginx
- 進入配置目錄
cd /usr/local/nginx/
- 啟動nginx
cd sbin #啟動nginx,啟動是沒有任何提示資訊的 ./nginx # 查詢是否安裝成功 ps -ef|grep nginx # 或者直接通路機器的ip位址,檢視是否能出現歡迎頁面結課
- 開放端口号
# 檢視開放的端口号 firewall-cmd --list-all #設定需要開放的端口号 firewall-cmd --add-service=http --permanent firewall-cmd --add-port=80/tcp --permanent #重新開機防火牆 firewall-cmd --reload # 使用iptables 的方法請參考下邊的 錯誤解決辦法 # 阿裡雲或者是騰訊雲伺服器的話直接在安全組政策添加就行了
錯誤集錦
執行報錯
./configure
… not found
Checking for C complier
錯誤原因:沒有安裝C編譯器
解決辦法
-
yum -y install gcc
-
yum -y install gcc-c++
-
yum -y install openssl openssl-devel
更多錯誤解決辦法,請參考 Centos安裝Nginx錯誤集錦
3. Nginx 常用指令
# 進入 nginx目錄
cd /usr/local/nginx/sbin/
#啟動
./nginx
#停止
./ngins -s stop
# 安全退出
./nginx -s quit
# 重新加載配置檔案
./nginx -s reload
# 檢視nginx程序
ps aux|grep nginx
4. Nginx配置
1. 配置檔案
官方原來的配置檔案
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#################以上為nginx的全局配置################
#最大連接配接數
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2. 自定義配置
主要用到的就是一下幾個内容,修改完成之後使用 nginx -s reload
重新加載 Nginx
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
########全局配置###########
events {
# 最大連接配接數
worker_connections 1024;
}
###http配置
http{
upstream lesscoding{
#負載均衡配置
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
}
server{
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://lesscoding;
}
#比對正規表達式的方式,請求中中帶有 test 就會轉發到這個位址,主要是PCRE的功勞
location ~ /test/ {
proxy_pass http://127.0.0.1:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server{
# 監聽8081端口
listen 8081;
server_name localhost;
}
}
3. 錯誤集錦
java.lang.IllegalArgumentException: The character [_] is never valid in a domain name.
解決辦法: 檢查
nginx.conf
中的
upstream
後邊跟的名字是否有非法字元
_
慘痛教訓:更改檔案之後一定要用
nginx -s stop
關閉後重新開機,或者是
nginx -s reload
重新整理配置檔案,而不是直接關閉指令視窗,否則就會出現莫名其妙的400錯誤
5. 前後端分離配置
什麼是動靜分離
Nginx 動靜分離簡單來說就是吧動态跟靜态請求分開,不能了解成隻是簡單的把動态頁面和靜态頁面實體分離,嚴格來說是把動态請求和靜态請求分開,可以了解成 Nginx處理靜态頁面,Tomcat處理動态頁面,大緻可以分為兩種
- 把靜态資源放在單獨的伺服器上
- 前後端一起釋出通過nginx分開
1. 前端打包
前端使用(:prod) 打包,将生成的
npm run build
目錄上傳到伺服器
dist
- 修改
nginx.conf
#最上邊的 user 修改為 root,但是也有人不建議用root賬戶,看個人需求吧
user root;
# 找到 server标簽下邊的 location
location / {
root #上傳之後的dist的路徑;
index index.html index.htm;
}
- 啟動nginx
# 進入nginx安裝目錄
cd /usr/local/nginx/sbin
#運作nginx
./nginx
2. 後端打包
後端使用 maven 打包成 jar包直接運作或者是達成 war 包放在tomcat容器裡邊運作
打 war 包請參考 https://blog.csdn.net/qq_42059717/article/details/119718896
- 後端打包
mvn clean package
- 後端運作jar 包
nohup java -jar demo.jar & nohup.out
3. 修改nginx配置檔案
這裡不知道放什麼,先放這個占着位置
- 修改配置檔案
http{
#權重越大,通路的幾率越大
upstream demo {
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=10;
}
server{
location / {
root /root/workspace/demo/dost;
index index.html index.htm;
}
location ~ /demo/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://demo/; #直接寫ip也行
}
}
}
- 重新開機nginx
# 進入 nginx安裝目錄
cd /usr/local/nginx/sbin
#重新加載配置檔案
./nginx -s reload
4. 後端部署war包配置
- 将 war 包放到 tomcat目錄下
#進入tomcat
cd /usr/local/tomcat/apache-tomcat
# 進入webapps目錄
cd webapps/
# 複制檔案到目前目錄
cp /root/woekspeace/demo/demo.war ./
# 或者使用mv指令進行移動
mv /root.workspace/demo/demo.war ./
- 啟動 tomcat
service tomcat start
這樣之後必須要在端口号之後加上我們的目錄名稱才能夠通路到我們的項目
- 修改 tomcat配置檔案
#進入tomcat的配置目錄
cd /usr/local/tomcat/apache-tomcat/conf
#編輯配置檔案
vim server.xml
#輸入/Host 或者是 ?Host快速定位
?Host
#按 i 進入編輯模式 在<Host>變遷内添加下列内容
<Context path="" docBase="/usr/local/src/apache-tomcat-7.0.106/webapps/demo" debug="0" privileged="true" reloadable="true"/>
# esc 退出編輯模式儲存退出
:wq!
#重新開機tomcat
service tomcat stop
service tomcat start
emo/demo.war ./
- 啟動 tomcat
service tomcat start
這樣之後必須要在端口号之後加上我們的目錄名稱才能夠通路到我們的項目
- 修改 tomcat配置檔案
#進入tomcat的配置目錄
cd /usr/local/tomcat/apache-tomcat/conf
#編輯配置檔案
vim server.xml
#輸入/Host 或者是 ?Host快速定位
?Host
#按 i 進入編輯模式 在<Host>變遷内添加下列内容
<Context path="" docBase="/usr/local/src/apache-tomcat-7.0.106/webapps/demo" debug="0" privileged="true" reloadable="true"/>
# esc 退出編輯模式儲存退出
:wq!
#重新開機tomcat
service tomcat stop
service tomcat start