日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

Nginx使用upstream实现动静分离

發布時間:2023/12/4 Nginx 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx使用upstream实现动静分离 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、為什么要進行動靜分離

分離資源,減少不必要到的請求消耗,減少請求延時。

注:我這里,是nginx處理靜態資源,apache處理動態資源。

場景分析:

1、未分離之前的場景步驟

(1)客戶端請求url到中間件(比如nginx,apache)

(2)中間件根據url請求相應目錄,程序框架

(3)程序框架運行程序邏輯

(4)程序邏輯請求相應數據資源

(5)將數據資源返回給客戶端

注:其實,靜態資源是不需要經過動態請求,直接中間件返回給客戶端就可以了。也就是說只要第1步和第5步就可以了

?

?

配置文件展示:

upstream php_api{#代理請求到本地apache服務器,實現動靜分離(這里我將apache默認端口更改為81)server 127.0.0.1:81; } server {listen 80;server_name www.xiaobudiu.top;access_log /etc/nginx/logs/access/www.xiabudiu.top.access.log main;root /data/www;location ~ \.php$ {#如果網站訪問的url后綴是.php,則代理使用apache進行解析proxy_pass http://php_api;index index.html index.htm;}#如果請求的是靜態資源,則默認使用nginx進行處理location ~ \.(jpg|png|gif)$ {expires 1h;gzip on;}location /{index index.html index.htm;}# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 404 403 /404.html;location = /404.html {root /data/errorPage;}location ~ /\.ht {deny all;} }

?

? 或者是這樣:

?

?

upstream image {server 192.168.0.3:80;server 192.168.0.4:80; }upstream php {server 192.168.0.5:80;server 192.168.0.6:80; }server {listen 80;server_name www.xiaobudiu.top;access_log /etc/nginx/logs/access/www.xiabudiu.top.access.log main;location /{#如果uri后綴不是.php或是圖片后綴,就走本地服務器進行處理root data/www;index index.php index.html;}location ~* \.php$ {#如果是.php結尾,反向代理到upstream php組里進行輪詢proxy_pass http://php;}location ~* "\.(.jpg|png|jpeg|gif)" {#如果是.jpg,.png,.jpeg,.gif結尾,反向代理到upstream image組里進行輪詢proxy_pass http://image;}# redirect server error pages to the static page /404.htmlerror_page 500 502 503 504 404 403 /404.html;location = /404.html {root /data/errorPage;}location ~ /\.ht {deny all;}}

注:這是在子配置文件中進行的定義,比如,上面編輯的就是/etc/nginx/conf.d/www.xiaobudiu.top.conf 文件

?

當然,由于nginx對代理有一定要求,所以,在nginx.conf中也要進行一定的定義,比如這樣:

nginx.conf

?

user nginx; worker_processes 1; worker_rlimit_nofile 65536;error_log /etc/nginx/logs/error/error.log warn; pid /var/run/nginx.pid;events {worker_connections 1024;multi_accept on;use epoll; }http {include /etc/nginx/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 /etc/nginx/logs/access/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;client_max_body_size 20m;gzip on;gzip_proxied any;gzip_comp_level 3;gzip_min_length 1k;gzip_buffers 16 32k;gzip_http_version 1.0;gzip_types text/plain text/css application/json application/xml+rss text/javascript image/jpeg image/gif image/png;fastcgi_buffers 256 16k;fastcgi_buffer_size 128k;fastcgi_connect_timeout 3s;fastcgi_send_timeout 120s;fastcgi_read_timeout 120s;reset_timedout_connection on;server_names_hash_bucket_size 100;include /etc/nginx/conf.d/*.conf;}

?

?

?

最后,需要說明的是,上述配置文件只是為了說明反向代理和負載均衡是如何實現的,并沒有結合實際項目。

?

注:nginx 官方proxy模塊文檔?http://nginx.org/en/docs/http/ngx_http_proxy_module.html

注:負載均衡中多態服務器間的數據同步這里采用rsync,當然,還有其他方式。可參考:

https://www.cnblogs.com/miclesvic/p/6189540.html

總結

以上是生活随笔為你收集整理的Nginx使用upstream实现动静分离的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。