Nginx簡介
Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,在BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度、京東、新浪、網易、騰訊、淘寶等。
優勢
Nginx 可以在大多數 Unix Linux OS 上編譯執行,並有 Windows 移植版。 Nginx 的1.20.0穩定版已經於2021年4月20日釋出,一般情況下,對於新建站點,建議使用最新穩定版作為生產版本,已有站點的升級急迫性不高。Nginx 的原始碼使用 2-clause BSD-like license。
Nginx 是一個很強大的高效能Web和反向代理服務,它具有很多非常優越的特性:
- 在連線高併發的情況下,Nginx是Apache服務不錯的替代品:Nginx在美國是做虛擬主機生意的老闆們經常選擇的軟體平臺之一。能夠支援高達 50,000 個併發連線數的響應。
- Nginx作為負載均衡服務:Nginx 既可以在內部直接支援 Rails 和 PHP 程式對外進行服務,也可以支援作為 HTTP代理服務對外進行服務。Nginx採用C進行編寫,不論是系統資源開銷還是CPU使用效率都比 Perlbal 要好很多。
- 處理靜態檔案,索引檔案以及自動索引,開啟檔案描述符緩衝。
- 無快取的反向代理加速,簡單的負載均衡和容錯。
- FastCGI,簡單的負載均衡和容錯。
- 模組化的結構。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCG或其它代理伺服器處理單頁中存在的多個 SSI,則這項處理可以並行執行,而不需要相互等待。
- 支援 SSL 和 TLSSNI。
- Nginx程式碼完全用C語言從頭寫成,已經移植到許多體系結構和作業系統,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows。Nginx有自己的函式庫,並且除了zlib、PCRE和OpenSSL之外,標準模組只使用系統C庫函式。而且,如果不需要或者考慮到潛在的授權衝突,可以不使用這些第三方庫。
- 代理伺服器。作為郵件代理服務:Nginx 同時也是一個非常優秀的郵件代理服務(最早開發這個產品的目的之一也是作為郵件代理伺服器),Last.fm 描述了成功並且美妙的使用經驗。
- Nginx 是一個安裝非常的簡單、配置檔案非常簡潔(還能夠支援perl語法)、Bug非常少的服務。Nginx 啟動特別容易,並且幾乎可以做到7*24不間斷執行,即使執行數個月也不需要重新啟動。你還能夠不間斷服務的情況下進行軟體版本的升級。
使用
下載
在官網下載最新穩定版
從原始碼構建
如果需要某些特殊功能,但軟體包和埠不提供這些功能,則也可以從原始檔編譯nginx。雖然更靈活,但是這種方法對於初學者來說可能很複雜。有關更多資訊,請參見從原始碼構建nginx。
安裝
下載完成後解壓到不含中文(切記!)的目錄
啟動
兩種方法:
直接雙擊該目錄下的"nginx.exe",即可啟動nginx伺服器;
命令列進入該資料夾,執行start nginx命令,也會啟動nginx伺服器。
使用 Win+R開啟執行,輸入cmd,然後按Enter
首先進入nigix所在的目錄,如下圖所示:
D:
cd Environment/nginx-1.20.0 #這是我解壓的目錄,替換成自己的即可
啟動:start nginx.exe
停止:nginx.exe -s stop
重新載入:nginx.exe -s reload
使用http://localhost:埠檢視
那麼怎麼知道自己的nginx是否啟動成功:開啟你的管理器,如在程序中看到兩個nginx說明啟動成功。
開啟瀏覽器輸入http://localhost
,顯示以下介面即表示啟動成功
反代理Tomcat
反向代理動態頁面
首先用Tomcat啟動一個JavaWeb專案,Tomcat埠號為8080,響應內容如下
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
Cookie[] cookies = req.getCookies();
if (cookies == null) {
out.write("First");
} else {
out.write("上次時間是:");
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("lastTime")) {
long l = Long.parseLong(cookies[i].getValue());
String s = new Date(l).toString();
out.write(s);
}
}
}
resp.addCookie(new Cookie("lastTime", System.currentTimeMillis()+""));
}
每次重新整理都會動態從Cooki中獲取上次時間,啟動後瀏覽器訪問http://localhost:8080/c1
即可檢視效果
然後編輯Nginx配置檔案,新增一個包
upstream tomcatserver {
server localhost:8080;
}
然後編輯server閉包,修改如下內容
Listen 80;
server_name localhost;
location / {
proxy_pass http://tomcatserver;
}
之後啟動Nginx,訪問localhost/c1即可預覽效果
則表示正確代理了動態頁面
Nginx的配置
配置檔案位於安裝目錄下的/conf/nginx.conf
檔案
server {
listen 80; #監聽的埠
server_name localhost; #監聽的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.html;
proxy_pass http://127.0.0.1:8081; #轉發請求的地址
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
Windows下的常用命令
啟動服務:start nginx
退出服務:nginx -s quit
強制關閉服務:nginx -s stop
過載服務:nginx -s reload
(過載服務配置檔案,類似於重啟,服務不會中止)
驗證配置檔案:nginx -t
使用配置檔案:nginx -c "配置檔案路徑"
使用幫助:nginx -h
Linux下的安裝
一、安裝編譯工具及庫檔案
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、首先要安裝 PCRE
PCRE 作用是讓 Nginx 支援 Rewrite 功能。
1、下載 PCRE 安裝包,下載地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
cd /usr/local/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2、解壓安裝包:
tar zxvf pcre-8.35.tar.gz
3、進入安裝包目錄
cd pcre-8.35
4、編譯安裝
./configure
make && make install
5、檢視pcre版本
pcre-config --version
安裝 Nginx
1、下載 Nginx,下載地址:https://nginx.org/en/download.html
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.20.0.tar.gz
2、解壓安裝包
tar zxvf nginx-1.20.0.tar.gz
3、進入安裝包目錄
cd nginx-1.20.0
4、編譯安裝
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make
make install
到此,nginx安裝完成。
Nginx 配置
nginx.conf說明
#user nobody;
worker_processes 1; #工作程序:數目。根據硬體調整,通常等於cpu數量或者2倍cpu數量。
#錯誤日誌存放路徑
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; # nginx程序pid存放路徑
events {
worker_connections 1024; # 工作程序的最大連線數量
}
http {
include mime.types; #指定mime型別,由mime.type來定義
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; #用log_format指令設定日誌格式後,需要用access_log來指定日誌檔案存放路徑
sendfile on; #指定nginx是否呼叫sendfile函式來輸出檔案,對於普通應用,必須設定on。如果用來進行下載等應用磁碟io重負載應用,可設著off,以平衡磁碟與網路io處理速度,降低系統uptime。
#tcp_nopush on; #此選項允許或禁止使用socket的TCP_CORK的選項,此選項僅在sendfile的時候使用
#keepalive_timeout 0; #keepalive超時時間
keepalive_timeout 65;
#gzip on; #開啟gzip壓縮服務
#虛擬主機
server {
listen 80; #配置監聽埠號
server_name localhost; #配置訪問域名,域名可以有多個,用空格隔開
#charset koi8-r; #字符集設定
#access_log logs/host.access.log main;
location / {
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$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。
# root html; #根目錄
# fastcgi_pass 127.0.0.1:9000; #請求轉向定義的伺服器列表
# fastcgi_index index.php; # 如果請求的Fastcgi_index URI是以 / 結束的, 該指令設定的檔案會被附加到URI的後面並儲存在變數$fastcig_script_name中
# 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; # ssl_prefer_server_ciphers on; #
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
啟動nginx服務
切換目錄到/usr/local/nginx/sbin下面
啟動nginx命令:
./nginx
檢視nginx服務是否啟動成功
ps -ef | grep nginx
訪問站點
從瀏覽器訪問我們配置的站點ip:
Nginx 其他命令
以下包含了 Nginx 常用的幾個命令:
啟動服務:./nginx
退出服務:nginx -s quit
強制關閉服務:nginx -s stop
過載服務:nginx -s reload
(過載服務配置檔案,類似於重啟,但服務不會中止)
驗證配置檔案:nginx -t
使用配置檔案:nginx -c "配置檔案路徑"
使用幫助:nginx -h