Nginx的反向代理和負載均衡
阿新 • • 發佈:2020-08-02
nginx實現反向代理
yy 複製,8yy:表示從當前游標所在的行開始複製8行
p 貼上
dd 剪下,8dd:表示從當前游標所在的行開始剪下8行
14 http { 15 include /etc/nginx/mime.types; 16 default_type application/octet-stream; 17 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"';21 22 access_log /var/log/nginx/access.log main; 23 24 sendfile on; 25 #tcp_nopush on; 26 27 keepalive_timeout 65; 28 29 #gzip on; 30 server { 31 listen 80; 32 server_name 192.168.65.10;33 location / { 34 root /home/web/html; 35 index choose.html; 36 } 37 } 38 server { 39 listen 8000; 40 server_name 192.168.65.10; 41 location / { 42 proxy_pass https://www.baidu.com; 43 }44 } 45 include /etc/nginx/conf.d/*.conf; 46 }
但在實際開發中,會使用nginx的分類配置,實現多個配置檔案同時生效,這樣就不用多個配置檔案駁雜在一起
如:
進入配置目錄
[root@localhost ~]# cd /etc/nginx/conf.d/ [root@localhost conf.d]# ls default.conf
建立百度配置檔案baidu.conf
[root@localhost conf.d]# vi baidu.conf
1 server { 2 listen 8000; 3 server_name 192.168.65.10; 4 location / { 5 proxy_pass https://www.baidu.com; 6 } 7 }
然後優雅的重啟 nginx -s reload ,效果和上面一樣,但配置可不一樣
nginx實現負載均衡
打包二個jar專案,裡面分別有二個頁面,放在一個目錄裡面