Nginx原理介紹以及教你Windows系統下快速上手(附常見問題解決方法)
前言
在網際網路專案中,當訪問量達到一定程度的時候,單臺伺服器並不能夠滿足使用者的請求,需要多臺伺服器,這時候就可以使用Nginx做反向代理,並且多臺伺服器可以平均分擔負載,儘量的避免出現某臺伺服器負載高而某臺伺服器閒置的情況出現,充分利用資源,降低企業成本。
什麼是Nginx
Nginx是一個http伺服器,可以獨立提供http服務。可以做網頁靜態伺服器。
虛擬主機,可以實現在一臺伺服器虛擬出多個網站
反向代理,在客戶端與伺服器端之間建立一個關卡
負載均衡,使資源利用最大化
怎麼在Windows下使用Nginx呢?
第一步、下載一個Nginx,網址我都準備好了,各位看官自便http://nginx.org/
第二步、直接解壓安裝,安裝完之後會變成這個樣子
第三步、先不著急改配置,我們先執行試試,
點選上圖exe檔案,即可實現執行哦,閃退表示執行成功,也可自己開啟資源管理器看看是否執行成功(如果不放心的話)
如下圖出現兩個標識就是執行成功了
第四步,修改配置檔案,配置檔案中每個配置的詳解我就直接貼出來了(conf資料夾下的ngnix.cof檔案)
PS:大家先看下配置檔案的具體含義,具體修改在下面
#user nobody;
# 指定nginx程序數
worker_processes 1;
# 全域性錯誤日誌及PID檔案
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# 連線數上限
worker_connections 1024;
}
#設定http伺服器,利用它的反向代理功能提供負載均衡支援
http {
#設定mime型別,型別由mime.type檔案定義
include 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 logs/access.log main;
# sendfile 指令指定 nginx是否呼叫sendfile 函式(zero copy 方式)來輸出檔案,對於普通應用
sendfile on;
#tcp_nopush on;
# 連線超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
#開啟gzip壓縮,壓縮html
#gzip on;
###################################
# 設定負載均衡的伺服器列表 支援多組的負載均衡,可以配置多個upstream 來服務於不同的Server.
# nginx 的 upstream 支援幾種方式的分配
# 1.輪詢(預設)每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
# 2.weight 指定輪詢機率,weight和訪問比率成正比,用於後端伺服器效能不均的情況。
# 3.ip_hash 每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題
# 4.fair
# 5.url_hash #Urlhash
upstream tomcat_server{
#weigth引數表示權值,權值越高被分配到的機率越大
#1.down 表示當前的server暫時不參與負載
#2.weight 預設為1 weight越大,負載的權重就越大
#3.backup 其他所有的非backup機器down或者忙的時候,請求backup機器,所以這臺機器的壓力最輕,備用機器
server localhost:8080 weight=55;
server localhost:8081 weight=44;
server localhost:8082 weight=1;
server 192.0.0.1 weight=1;
}
# 配置代理伺服器的地址,即Nginx安裝的伺服器地址、監聽埠、預設地址
server {
#1.監聽80埠
listen 80;
#對於server_name,如果需要將多個域名的請求進行反向代理,可以配置多個server_name來滿足要求
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 預設主頁目錄在nginx安裝目錄的html子目錄
root html;
index index.html index.htm;
proxy_pass http://tomcat_server;
}
location ~\.(html|js|css|png|gif)${
root F:\LoveToTravel\apache-tomcat-1\apache-tomcat-7.0.85\webapps\ROOT
}
#訪問本地E:/source資料夾 訪問路徑為localhost:8099/file/a.png 實際訪問路徑為 E:/source/file/a.png
# location /file/ {
# root E:/source/;
# autoindex on;
# }
#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$ {
# proxy_pass http://127.0.0.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;
# }
#}
}
該如何去修改呢?(以配置tomcat叢集為例項)
自己在upstream 後定義一個叢集,策略按自己需求自定義
配置Ngnix預設開啟路徑(筆者的是Ngnix目錄下的html目錄下)
指定代理的叢集
第五步、在瀏覽器輸入localhost:8080,如果能訪問到你指定的目錄,表示執行成功
第六步、常見BUG詳解
一、配置修改未生效:重啟Ngnix(初學者可直接進工作管理員結束程序)
二、埠被佔用,Windows系統下具有一個預設的瀏覽器佔用著80埠,將其修改就OK
轉賬請註明出處,掌聲送給社會人