1. 程式人生 > 程式設計 >nginx反向代理、負載均衡配置及vue專案部署

nginx反向代理、負載均衡配置及vue專案部署

nginx配置檔案結構

Nginx的配置檔案是一個純文字檔案,它一般位於Nginx安裝目錄的conf目錄下,整個配置檔案是以block的形式組織的。每個block一般以一個大括號“{”來表示。block可以分為幾個層次,整個配置檔案中Main命令位於最高層,在Main層下面可以有Events、 HTTP等層級,而在HTTP層中又包含Server層,即server block,

Nginx 服務的基本配置項詳解

我的本機nginx配置檔案路徑是 /usr/local/nginx/conf/ 這個目錄下面,其中nginx.conf是主配置檔案。Nginx.conf配置檔案主要分成四個部分:

main (全域性設定) 。

sever (主機設定)。

upstram負載均街伺服器設定。

location(URL匹配特定位置的設定)。

說明

main部分設定的命令將影響其他所有設定。 server 部分的命令主要用於指定主機和埠;。upstream命令主要用於負載均衡,設定一系列的後端伺服器。 location 部分用於匹配網頁位置。這四者之間的關係式server繼承main, location 繼承server, upstream 既不會繼承其他設定也不會被繼承。

在這四個部分當中,每個部分都包含若干命令,這些命令主要包含Nginx的主模組命令,事件模組命令,HTTP核心模組命令,同時每個部分還可以使用其他HTTP模組命令(例如HTTP SSL模組,HttpGzip Static模組和Http Addition模組等)。

下面通過一個Nginx配置例項詳細介紹nginx.conf每個命令的含義。

#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 {
    use epoll;
    worker_connections  1024;
}

/*
  以上這塊配置程式碼是對nginx全域性屬性的配置。
  user :主模組命令, 指定Nginx的worker程式執行使用者以及使用者組,預設由nobody賬號執行。
  worker processes: 指定Nginx要開啟的程式數。
  error log
:用來定義全域性錯設日誌檔案的路徑和日誌名稱。日誌輸出級別有debug,info,notice,warn,error,crit 可供選擇,其中debug輸出日誌最為詳細,面crit輸出日誌最少。 pid: 用來指定程式id的儲存檔案位置。 event:設定nginx的工作模式及連線數上限,其中引數use用來指定nginx的工作模式(這裡是epoll,epoll是多路複用IO(I/O Multiplexing)中的一種方式),nginx支援的工作模式有select,poll,kqueue,epoll,rtsig,/dev/poll。其中select和poll都是標準的工作模式,kqueue和epoll是高效的工作模式,對於linux系統,epoll是首選。 worker_connection是設定nginx每個程式最大的連線數,預設是1024,所以nginx最大的連線數max_client=worker_processes * worker_connections。程式最大連線數受到系統最大開啟檔案數的限制,需要設定ulimit。 */ #下面部分是nginx對http伺服器相關屬性的設定 http { include mime.types; 主模組命令,對配置檔案所包含檔案的設定,減少主配置檔案的複雜度,相當於把部分設定放在別的地方,然後在包含進來,保持主配置檔案的簡潔 default_type application/octet-stream; 預設檔案型別,當檔案型別未定義時候就使用這類設定的。 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 指定nginx日誌的格式 # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; 開啟高效檔案傳輸模式(zero copy 方式),避免核心緩衝區資料和使用者緩衝區資料之間的拷貝。 #tcp_nopush on; #keepalive_timeout 0; 客戶端連線超時時間 keepalive_timeout 65; #gzip on; 設定是否開啟gzip模組 #server段是虛擬主機的配置 這裡可以寫在別的檔案中 然後在包含進來,比如寫在/usr/local/nginx/vhost/xxx.conf 目錄某檔案中 然後在包含進來,可以包含多個檔案 include /usr/local/nginx/vhost/*; server { listen 80; 虛擬主機的服務埠 server_name localhost; 用來指定ip或者域名,多個域名用逗號分開 #charset koi8-r; #access_log logs/host.access.log main; location / { 地址匹配設定,支援正則匹配,也支援條件匹配,這裡是預設請求地址,使用者可以location命令對nginx進行動態和靜態網頁過濾處理 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$ { 將以php為字尾的檔案轉發到 FastCGI處理. 使用FastCGI預設配置。本地8088埠處理 fastcgi_pass http://127.0.0.1:8088; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #靜態檔案,nginx自己處理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/public/images; expires 30d; #快取時間30天,靜態檔案更新不多,過期時間可以設大一點。 } #配置Nginx狀態的地址 location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } #配置nginx負載均衡的伺服器列表 upstream mysvr { #weigth引數表示權值,權值越高被分配到的機率越大 #本機上的Squid開啟3128埠 server 192.168.199.1:88 weight=5; server 192.168.199.1:80 weight=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; # } #} } 複製程式碼

現在來實踐一下

這裡監聽是是80埠,是nginx的預設埠,在配置檔案中就新增proxy_pass配置,修改server_name後面的值為_,儲存,重啟nginx讓配置生效,瀏覽器開啟輸入伺服器ip就會跳轉到到百度首頁。這就是簡單的反向代理實現。

負載均衡

接著如下配置就可以是一個簡單的負載均衡,預設是輪詢策略,即將請求平分到每臺伺服器

部署vue專案

將打包好的vue的dist目錄複製到/home/myblog目錄下

接下來開啟nginx的配置檔案nginx.conf,修改配置如下

儲存退出,重啟nginx,用在瀏覽器用伺服器ip或者繫結的域名就可訪問,dist檔案下的index.html是不可以直接用瀏覽器開啟訪問的

個人網站:www.panbingwen.cn