共計 6559 個字符,預計需要花費 17 分鐘才能閱讀完成。
本篇內容主要講解“Nginx 的概念是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“Nginx 的概念是什么”吧!
1. Nginx 的概念?
「什么是 Nginx 呢:」
Nginx 是一個基于 HTTP 的反向代理服務器,也是一個基 IMAP/POP3/SMTP 服務郵件服務器
反向代理服務器:現在我們 A 需要訪問的目標 B 服務器的 10.7.182.100,我要訪問這個 B 服務器上的資源,現在如果使用了 Nginx 之后,我可以通過 Nginx 服務器從而達到訪問 10.7.182.100 這個服務器的目的
IMAP/POP/SMTP:這三個是郵件的傳輸協議
郵件服務器:發送郵件 接收郵件
Web 服務器:本身是一個 Web 服務器的軟件,類似于 Tomcat 這種 Web 服務的軟件
「Nginx 能干什么呢:」
可以作為 Web 服務器
可以作為郵件服務器
可以作為反向代理的服務器
動靜分離 (就是將動態資源和靜態資源分隔開)
可以實現負載均衡
2、Nginx 的安裝
「Nginx 安裝步驟:」
第一步: 下載我們的 nginx 這里以 1.6.2 版本為例 第二步: 共享安裝文件到我們的 linux 上面 第三步: 將文件拷貝到 /usr/local 下面 第四步: 安裝 tar -zxvf xxxx.tar.gz 第五步: 下載所需要的依賴庫 yum install pcre yum install pcre-devel yum install zlib yum install zlib-devel 第六步: 進行 config 的配置 cd nginx-1.6.2 ./configure --prefix=/usr/local/nginx 第七步: 安裝 make make install 第八步: 啟動 nginx /usr/local/nginx/sbin/nginx 關閉: .... -s stop -s reload 查看端口是否有問題 netstat -tunpl |grep 80 瀏覽器進行驗證沒問題就可以
3、Nginx 的配置文件的解析
「配置文件:」
#user nobody; #工作的線程 (4 核 8 線程那么下面就設置成 8 物理硬件有關) worker_processes 1; #全局的錯誤日志存放的地方 #error_log logs/error.log; #全局錯誤日志存放的地方 后面的 notice 表示的是輸出錯誤日志的格式 #error_log logs/error.log notice; #error_log logs/error.log info; #nginx 進程號存放的地方 #pid logs/nginx.pid; #最大的連接數 (這個也跟硬件是有關系的) events { worker_connections 1024; } #下面就是跟 HTTP 請求有關系的了 http { #表示的是當前服務器支持的類型 include mime.types; #默認傳輸的數據類型是流 default_type application/octet-stream; # 聲明了一種日志格式 這種日志格式的名字叫做 main log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for #表示的是每一次請求的日志記錄 格式就是上面的 main 格式 access_log logs/access.log main; #是否可以發送文件 sendfile on; #tcp_nopush on; #這個是雙活的超時的時間 #keepalive_timeout 0; keepalive_timeout 65; #是否打開文件的壓縮 #gzip on; #虛擬一個主機 #負載均衡的配置 upstream qianyu { ip_hash; server 10.7.182.110:8080; server 10.7.182.87:8080; } #server { # listen 90; # server_name localhost; # location / { # root qianyu; # index qianyu.html; # } #做一個反向代理 #表示的是 如果你訪問的后綴是 .jpg 結尾的話那么 就訪問下面的另外的服務器 # location ~ \.jpg$ { # proxy_pass http://10.7.182.110:8080; # } # 下面要演示一個負載均衡的例子 #location ~ \.jpg$ { # proxy_pass http://qianyu; #} #} #虛擬一個動靜分離的機器 server { listen 9999; server_name localhost; #表示的是動態資源訪問的機器 location / { proxy_pass http://10.7.182.54:8080; } #表示的是非 css 和 js 文件訪問的地址 location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { root /usr/local/webapp; expires 30d; } #表示的是 css 和 js 的訪問地址 location ~ .*\.(js|css)?$ { root /usr/local/webapp; expires 1h;
} } #虛擬了服務器 server { #端口是 80 listen 80; #這個表示的是虛擬服務器默認訪問的是本機 server_name localhost; #這個是虛擬服務器的編碼 #charset koi8-r; #當前服務器的日志的存儲的地方 #access_log logs/host.access.log main; #做了一個地址的映射 location / { root html; index index.html index.htm; } #如果報 404 的時候訪問的頁面 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html #報錯 50 開頭的是 就訪問 50x.html error_page 500 502 503 504 /50x.html; # 下面對 50x.html 又做了一個映射 就訪問根目錄下的 html 中的 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; # } #} }
4、實現 Nginx 下的反向代理
「做一個反向代理:」
表示的是如果你訪問的后綴是 .jpg 結尾的話,那么就訪問下面的另外的服務器
location ~ \.jpg$ { proxy_pass http://10.7.182.110:8080; }
5、實現 Nginx 下的負載均衡
「第一種策略:默認是輪循的策略:」
配置 upstream upstream qianyu { server 10.7.182.110:8080; server 10.7.182.87:8080; } 配置負載均衡 location ~ \.jpg$ { proxy_pass http://qianyu; }
「第二種策略:權重(weight):」
# 負載均衡的配置 upstream qianyu { server 10.7.182.110:8080 weight=2; server 10.7.182.87:8080 weight=1; } 配置負載均衡 location ~ \.jpg$ { proxy_pass http://qianyu; }
「第三種策略:IPHash 的使用:」
# 負載均衡的配置 upstream qianyu { ip_hash; server 10.7.182.110:8080; server 10.7.182.87:8080; } 配置負載均衡 location ~ \.jpg$ { proxy_pass http://qianyu; }
6、實現 Nginx 下的動靜分離
「動靜分離:」
簡單的來說就是將動態資源和靜態資源給分隔開
靜態資源放到 Nginx 服務器上
動態資源放到 Tomcat 服務器上
Jpg html css png gif .... 靜態資源 ---- 放到 Nginx 服務器上 .action 結尾的都是動態的資源 ------------------------- 放到 Tomcat 的服務器上
「動靜分離的實現:」
在 /usr/local 目錄下創建 webapp 文件夾
在 webapp 目錄下創建 css、html、js、img 文件夾
編寫 HTML 的內容并將 html 文件放到 webapp 目錄下
!DOCTYPE html
html
head
meta charset= utf-8 /
title /title
link rel= stylesheet type= text/css href= /css/main.css /
script src= /js/main.js type= text/javascript charset= utf-8 /script
/head
body
img src= /img/bbb.jpg /
br /
a href= /Dynamic_Resource/qianyu.action 點擊我訪問動態資源 /a
/body
/html
將圖片放到 img 目錄下,將 css 放到 css 目錄下,將 js 文件放到 js 的目錄下
編寫動態資源的這個工程
package com.qy.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class qianyuServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( text/html;charset=utf-8 PrintWriter writer=response.getWriter(); writer.write(this is dynamic resource test writer.flush(); writer.close(); } }
編寫配置文件 /conf/nginx.xml 文件
#虛擬一個動靜分離的機器 server { listen 9999; server_name localhost; #表示的是動態資源訪問的機器 location / { proxy_pass http://10.7.182.54:8080; } #表示的是非 css 和 js 文件訪問的地址 location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { root /usr/local/webapp; expires 30d; } # 表示的是 css 和 js 的訪問地址 location ~ .*\.(js|css)?$ { root /usr/local/webapp; expires 1h;
測試
7、虛擬主機
「虛擬主機配置:」
server { listen 90; server_name localhost; location / { root qianyu; index qianyu.html; } #做一個反向代理 # 表示的是 如果你訪問的后綴是 .jpg 結尾的話那么 就訪問下面的另外的服務器 location ~ \.jpg$ { proxy_pass http://10.7.182.110:8080; }
}
到此,相信大家對“Nginx 的概念是什么”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!