nginx之Http代理和Websocket代理詳解
1.nginx安裝
按照nginx官方的教程,在Ubuntu上安裝nginx
http://nginx.org/en/linux_packages.html#Ubuntu
按照上面的安裝,會(huì)為我們注冊(cè)nginx的service,我們可以通過(guò)service nginx start來(lái)啟動(dòng)nginx
2.nginx配置文件
默認(rèn)的配置文件是:nginx.conf,一般存放的位置是/usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.
3.nginx常用命令
nginx啟動(dòng)之后,我們可以使用-s參數(shù)來(lái)執(zhí)行一些控制命令
3.1 幫助命令
nginx -? nginx -h
3.2 快速停止
nginx -s stop
3.3 優(yōu)雅停止
nginx -s quit
3.4 重新加載配置文件
nginx -s reload
一旦主進(jìn)程接收到重新加載配置的信號(hào),它將檢查新的配置文件的語(yǔ)法有效性,并嘗試應(yīng)用其中的配置。
如果應(yīng)用新配置成功,主進(jìn)程將啟動(dòng)新的工作進(jìn)程,并向舊的工程發(fā)送消息,請(qǐng)求將它們關(guān)閉。
如果新配置失敗,主進(jìn)程將回滾配置更改并繼續(xù)使用舊配置。
3.5 重新打開日志文件
nginx -s reopen
3.6 nginx主進(jìn)程id
nginx主進(jìn)程的id將寫在/usr/local/nginx/logs或/var/run目錄中的nginx.pid文件中
我們也可以通過(guò)以下命令來(lái)殺死nginx主進(jìn)程:
# pid表示主進(jìn)程id kill -s quit pid
3.7 查看nginx進(jìn)程
ps -ax | grep nginx
3.8 以指定的配置文件運(yùn)行
nginx -c file
4. nginx靜態(tài)代理
server {
listen 80; # 監(jiān)聽的端口
server_name localhost; # 服務(wù)名稱,對(duì)應(yīng)的ip或者域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { # 如果訪問(wèn)localhost,請(qǐng)求將會(huì)被轉(zhuǎn)發(fā)到nginx根目錄下的html文件夾中的index.html或者index.htm
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 { # 服務(wù)器錯(cuò)誤將會(huì)被轉(zhuǎn)發(fā)到nginx根目錄下的html文件夾中的50x.html
root html;
}
}5 nginx做代理服務(wù)器
將localhost:8082/api/test請(qǐng)求轉(zhuǎn)發(fā)到http://192.168.1.92:8080/test
proxy_pass http://192.168.1.92:8080/;
結(jié)尾的/必須有,否則請(qǐng)求會(huì)被轉(zhuǎn)發(fā)到http://192.168.1.92:8080/api/test
http {
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 8082;
server_name localhost;
location /api/ {
proxy_pass http://192.168.1.92:8080/;# 這個(gè)結(jié)尾的/必須有,否則請(qǐng)求會(huì)被代理到http://192.168.1.92:8080/api/test
}
}
}6.負(fù)載均衡
負(fù)載均衡的策略有輪詢、最少連接數(shù)、iphash、權(quán)重。默認(rèn)使用的負(fù)載均衡策略是輪詢。
6.1 輪詢
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}6.2 最少連接
http {
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}6.3 ip hash
http {
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}6.4 權(quán)重
http {
upstream myapp1 {
server srv1.example.com weight=3;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}6.5相關(guān)參數(shù)
# 設(shè)置server的權(quán)重值,默認(rèn)是1 weight=number # 最大連接數(shù),限制代理服務(wù)器的最大連接數(shù),默認(rèn)值為0,表示沒有限制 max_conns=number # 服務(wù)器的不可用時(shí)間,當(dāng)連接失敗時(shí) fail_timeout=time # 將server標(biāo)記為備用,當(dāng)主server不可用時(shí),將請(qǐng)求備用server backup # 將server標(biāo)記為停止 down
7.websocket代理
http {
upstream backend {
server srv1.example.com;
}
server {
listen 80;
location /chat/ {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx安裝后/etc/nginx/conf.d下沒有default.conf的解決
nginx.conf是nginx默認(rèn)加載的配置文件 通過(guò)nginx -V可以看nginx默認(rèn)配置文件路徑,本文主要介紹了Nginx安裝后/etc/nginx/conf.d下沒有default.conf的解決,感興趣的可以了解一下2023-11-11
解決Nginx location中配置proxy_pass轉(zhuǎn)發(fā)時(shí)斜線‘/‘導(dǎo)致404問(wèn)題
這篇文章主要介紹了解決Nginx location中配置proxy_pass轉(zhuǎn)發(fā)時(shí)斜線‘/‘導(dǎo)致404問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
nginx主動(dòng)健康檢查功能實(shí)現(xiàn)
nginx_upstream_check_module是一個(gè)Nginx的第三方模塊,它可以實(shí)現(xiàn) Nginx的主動(dòng)健康檢查功能,本文將介紹一個(gè)基于 Nginx 的第三方模塊 nginx_upstream_check_module,它可以實(shí)現(xiàn) Nginx 的主動(dòng)健康檢查功能,可以幫助我們更加有效地管理后端服務(wù)器,需要的朋友可以參考下2023-05-05
nginx禁止直接通過(guò)ip進(jìn)行訪問(wèn)并跳轉(zhuǎn)到自定義500頁(yè)面的操作
這篇文章主要介紹了nginx禁止直接通過(guò)ip進(jìn)行訪問(wèn)并跳轉(zhuǎn)到自定義500頁(yè)面的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
使用Nginx實(shí)現(xiàn)反向代理、配置負(fù)載均衡詳解
Nginx是一款輕量級(jí)的Web服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,其特點(diǎn)是占有內(nèi)存少,并發(fā)能力強(qiáng),文章詳細(xì)介紹了Nginx的安裝、配置、命令、靜態(tài)資源部署、反向代理和負(fù)載均衡等具體應(yīng)用2026-02-02

