欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx之Http代理和Websocket代理詳解

 更新時(shí)間:2025年03月05日 14:28:19   作者:fkjavaer  
本文介紹了在Ubuntu上安裝和配置Nginx的步驟,包括啟動(dòng)、停止、重新加載配置、重新打開(kāi)日志文件、查看進(jìn)程等常用命令,還詳細(xì)介紹了Nginx的靜態(tài)代理和負(fù)載均衡功能,包括輪詢、最少連接數(shù)、iphash和權(quán)重等策略

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 重新打開(kāi)日志文件

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)聽(tīng)的端口
        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,表示沒(méi)有限制
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)文章

最新評(píng)論