一、Nginx介紹
Nginx是由俄羅斯人開發的一款高性能的Web和反向代理服務器,它也可以作爲電子郵件的反向代理服務器。其以穩定、併發能力強、佔用資源少等顯著特點備受廣大互聯網公司青睞。
Nginx作爲Web服務器來使用可能大家都很清楚這一點,比如我們在發佈一些靜態網站的時候,通常都會選擇使用Nginx,而不會去選擇一些專門爲動態網站提供支持的服務器如Tomcat等。作爲反向代理服務器來使用也是它非常擅長的一個點,如果不瞭解反向代理機制的話可以去搜索一下,大致的意思是:用戶發送一個請求給網站,以期獲得一個響應,接收用戶請求的就是Nginx服務器,但Nginx不會自己來處理這個請求,它會根據用戶的不同請求類型,分發給不同的服務器去實現這個功能,然後將結果返回給用戶。在這一過程,看似是向一個服務器發送請求,實則背後有無數的服務器在提供支持。
以上,便是對Nginx的大致介紹,詳細介紹還得要去他的 Nginx官網才行。
二、Ubuntu下安裝Nginx
此次選用的Ubuntu版本是 Ubuntu 18.04.4 LTS。在Linux平臺安裝Nginx,有兩種方式,其一可以通過不同Linux發行版的默認軟件安裝包來進行安裝,其二就是自己下載Nginx源碼,自行編譯之後進行安裝。兩種方式安裝的Nginx性能上並無差異,要說有差別,可能就是安裝後的程序目錄(例如:執行程序、配置文件的目錄等)有一些差異,僅此而已。使用安裝包方式安裝的Nginx,其程序目錄分佈更符合Linux的整體系統結構的分佈安排,而以自行編譯方式安裝的則比較自由一些。
2.1.默認安裝包安裝方式:
這種方式較爲簡單,只需要輸入幾條命令,就可以完成安裝。
1)更新軟件倉庫源列表,使其保持最新的狀態
sudo apt-get update
2)安裝Nginx(安裝過程中會提示你同意佔用內存,確認即可)
sudo apt-get install nginx
至此,Nginx安裝完成,然後再對其進行簡單的配置就可以使用了。在進行配置之前,需要了解這種方式下安裝的默認程序文件位置分佈情況,具體如下:
1)所有的配置文件都在/etc/nginx下。
2)執行程序文件在/usr/sbin/nginx。
3)日誌文件放在了/var/log/nginx中。分別是access.log和error.log
4)默認虛擬主機的目錄配置在了/var/www/下面。這個目錄位置的設定是在/etc/nginx/sites-available裏的配置文件進行的。與虛擬主機相關的設置,都是在這裏進行的,可以自行修改,重啓Nginx即可生效。
3)啓動、關閉、重啓Nginx服務
1)sudo systemctl start nginx
2)sudo systemctl restart nginx
3)sudo systemctl stop nginx
需要注意的地方:
如果正確配置了配置文件,啓動後訪問不到網站,記得查看防火牆規則,看是不是相關的端口未對外開放(Nginx默認的是80端口)。
2.2.源碼安裝方式:
這種安裝方式需要我們自己下載Nginx程序源碼進行編譯安裝,相較於上一種方式來說比較繁瑣,對Linux新手來說可能還會出錯,好處就是配置比較靈活,自己可以設置程序的安裝位置、配置文件的位置等等。
既然我們要編譯Nginx的源碼,那麼編譯環境我們是必要的要準備好的,Nginx的編譯過程會有一些依賴包,編譯之前也得要下載安裝好。另外,還需要注意版本的選擇問題,一般選擇最新的穩定發行版即可。
1)編譯環境(gcc)、依賴庫(pcre,zlib,SSL)的安裝(Ubuntu和其他的發行版Linux的命令可能有一些差別)
sudo apt-get update
sudo apt-get install build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl
2)Nginx安裝(使用wget方式下載源碼,解壓之後進行編譯、安裝)
1)下載源碼至目錄 /usr/local/src(Nginx源碼下載目錄可以自己定),並進行解壓:
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
2)編譯源碼
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_ssl_module
( ./configure 主要作用是對即將安裝的軟件進行配置,檢查當前的環境是否滿足安裝軟件的依賴條件,生成makefile文件,以便你可以用make和make install來編譯和安裝程序。
–prefix=**,是設置安裝路徑.–with-http_ssl_module,使用https協議模塊。默認情況下,該模塊沒有被構建。建立並運行此模塊的OpenSSL庫是必需的。)
make
3)安裝程序
make install
3)配置
安裝成功之後,Nginx的程序目錄如下圖所示:
默認情況下,conf文件夾存放的是配置文件,html文件夾是默認創建的虛擬主機的網站目錄,logs是存放日誌文件,sbin是nginx的執行程序所在目錄。
4)啓動Nginx
#方法1(任意目錄)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#方法2(切換至程序文件所在目錄)
cd /usr/local/nginx/sbin
./nginx
5)重啓、關閉Nginx
切換至 nginx的程序文件所在目錄/usr/local/nginx/sbin/,執行如下命令:
./nginx -s reload
./nginx -s stop
三、Nginx配置優化
Nginx安裝完畢之後,在使用之前,我們得要先進行一番配置後纔可以使用。配置優化的目的是讓Nginx儘可能以最優的方式利用系統資源,提供高效服務。 注意,修改完配置文件,需要重啓Nginx才能夠生效。
兩種不同安裝方式的配置文件所在位置不一樣,需要按照上訴內容進行尋找。方式一的配置文件是/etc/nginx/default.conf,方式二爲:/usr/local/nginx/conf/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 {
worker_connections 1024;
}
http {
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 on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/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$ {
# 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;
# }
#}
}
上面的文件的結構爲:
... #全局塊
events { #events塊,工作模式及併發量設置
...
}
http #http塊,主要是設定和一次HTTP請求處理有關的配置
{
... #http全局塊
server #server塊,設定虛擬主機配置
{
... #server全局塊
location [PATTERN] #location塊
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局塊
}
上面的模塊結構說明:
1、全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日誌存放路徑,配置文件引入,允許生成worker_process數等。
2、events塊:配置影響nginx服務器與用戶的網絡連接處理。具體有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啓多個網絡連接序列化等。
3、http塊:主要是設定和一次HTTP請求處理有關的配置,可以嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。
4、server塊:配置虛擬主機的相關參數,一個http中可以有多個server。
5、location塊:配置請求的路由,以及各種頁面的處理情況。
以上就是這幾個模塊的作用所在,層次感還是很清楚的,具體到我個人的網站節點配置,配置信息如下:
#user nobody; #運行用戶,可以不進行設置
worker_processes 2; #啓動進程數量,通常設置成和cpu的數量相等
#全局錯誤日誌,這個設置可以放入全局塊,http塊,server塊,級別依次爲:debug|info|notice|warn|error|crit|alert|emerg
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#指定nginx進程運行文件存放位置
pid logs/nginx.pid;
#工作模式及併發量設置
events {
#epoll是多路複用IO(I/O Multiplexing)中的一種方式,僅用於linux2.6以上內核,可以大大提高nginx的性能
use epoll;
#設置網路連接序列化,防止驚羣現象發生,默認爲on
accept_mutex on;
#設置一個進程是否同時接受多個網絡連接,默認爲off
multi_accept on;
#單個後臺worker process進程的最大併發連接數。併發總數爲worker_processes 和 worker_connections 的乘積,
#設置了反向代理的情況下,併發總數會有所變化。
worker_connections 1024;
}
#http請求處理塊。主要是設定和一次HTTP請求處理有關的配置
http {
#文件擴展名與文件類型映射表,設定mime類型,類型由mime.type文件定義
include mime.types;
#默認文件類型,默認爲text/plain
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 方式)來輸出文件,
#對於普通應用,必須設爲 on,
#如果用來進行下載等應用磁盤IO重負載應用,可設置爲 off,
#以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
sendfile on;
#tcp_nopush on;
#連接超時時間,默認爲75s,可以在http,server,location塊。
keepalive_timeout 65;
#開啓gzip壓縮
gzip on;
#設定虛擬主機配置,一個HTTP模塊可以設置多個虛擬主機
server {
#監聽的端口
listen 80;
#監聽地址
server_name localhost;
#charset koi8-r;
#設定本虛擬主機的訪問日誌
access_log logs/host.access.log main;
#默認請求
location / {
#網站根目錄位置
root /home/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$ {
# 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
#
#禁止訪問 .htxxx 文件
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 配置,主要是設定和一次HTTPS請求處理有關的配置主要多了個SSL,其他的和HTTP差不多
# 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;
# }
#}
四、總結
之前一直有用過Nginx作爲靜態資源的服務器、反向代理服務器來使用,目前來看,這個服務器的性能還是非常穩定的,佔用的系統資源也不高,配置起來也方便。鑑於這麼多優點,最近在搭建Hexo博客,於是就用了這個服務器。也就順便總結一下和Nginx有關的一些東西。
配置Nginx的過程中,參考學習了以下文章,在此表示感謝:
1, https://www.nginx.cn/76.html
2,https://www.cnblogs.com/knowledgesea/p/5175711.html