1. Nginx介紹
- Nginx是一款輕量級的高性能的Web服務器和反向代理服務器以及電子郵件(IMAP/POP3)代理服務器。
- 反向代理是指以代理服務器來接收Internet上的鏈接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給Internet上請求連接的客戶端,此時代理服務器就對外表現爲一個反向代理服務器。
- Nginx的特點是內存和CPU佔用率低),高併發處理能力非常好(官方測試爲5w併發請求),反向代理性能非常好(可用於負載均衡)。
2. Nginx環境搭建
2.1 安裝所需環境
- 安裝編譯環境
[root@CentOS7 ~]# yum -y install gcc gcc-c++
- 安裝pcre庫(支持rewrite庫)
[root@CentOS7 ~]# yum -y install pcre pcre-devel
- 安裝zlib庫
[root@CentOS7 ~]# yum -y install zlib zlib-devel
- 安裝openssl
[root@CentOS7 ~]# yum -y install openssl openssl-devel
2.2 安裝Nginx
- 下載Nginx
[root@CentOS7 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
- 解壓源碼包
[root@CentOS7 ~]# tar -zxvf nginx-1.16.1.tar.gz
- 使用configure命令創建makeFile文件
[root@CentOS7 ~]# cd nginx-1.16.1/
[root@CentOS7 nginx-1.15.9]#./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
- 編譯及安裝
[root@CentOS7 nginx-1.16.1]# make -j 4 && make install
2.3 測試
啓動nginx,通過瀏覽器訪問
[root@CentOS7 ~]# /usr/local/nginx/sbin/nginx
3. Nginx常用命令
所有命令均在nginx安裝目錄下的sbin文件夾下執行,也就是/usr/local/nginx/sbin文件夾。
- ./nginx:普通啓動
- ./nginx -c /usr/local/nginx/conf/nginx.conf :通過配置文件啓動nginx進程
- ./nginx -s signal:發送信號到主進程(signal可以爲stop/quit/reopen/reload)。reopen相當於USR1,quit相當於QUIT,stop相當於INT
- ./nginx -v: 查看版本
- ./nginx -t :測試配置文件是否正確
- ./nginx -h :查看幫助信息
4. Nginx信號控制
Nginx的信號控制
語法:kill -信號選項 nginx的主進程號
-TERM,-INT |
快速關閉 |
-QUIT |
優雅的關閉進程,即等請求結束後再關閉 |
-HUP |
改變配置文件,平滑的重讀配置文件 |
-USR1 |
重讀打開日誌文件,在日誌按月/日分割時有用 |
-USR2 |
平滑的升級可執行程序 |
-WINCH |
優雅關閉舊的進程(配合USR2來進行升級) |
默認,nginx將其主進程的pid寫入到/usr/local/nginx/nginx.pid文件中。
5. Nginx配置文件詳解
默認nginx配置文件位於/usr/local/nginx/conf/nginx.conf。
1
2 #user nobody; #配置worker進程運行用戶
3 worker_processes 1; #nginx子進程數(worker process),最大爲物理CPU數*核數
4
5 #error_log logs/error.log; #配置全局錯誤日誌文件(級別有debug| info| notice| warn| error| crit),默認error
6 #error_log logs/error.log notice;
7 #error_log logs/error.log info;
8
9 #pid logs/nginx.pid; #進程PID存放位置
10
11
12 events { #配置Nginx連接的特性
13 worker_connections 1024; #單個後臺worker process進程的最大連接數
14 }
15
16
17 http { #這裏是配置http服務器的主要段
18 include mime.types; #文件擴展名與類型映射表
19 default_type application/octet-stream; #默認文件類型
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"'; #設定日誌格式
24
25 #access_log logs/access.log main; #配置access.log日誌及存放路徑,並使用上面定義的main格式
26
27 sendfile on; #是否開啓高效傳輸模式
28 #tcp_nopush on; #是否防止網絡阻塞
29
30 #keepalive_timeout 0;
31 keepalive_timeout 65; #連接超時時間,單位s
32
33 #gzip on; #是否開啓gzip壓縮
34
35 server { #設定虛擬主機配置
36 listen 80; #指定虛擬主機監聽的端口
37 server_name localhost; #指定虛擬主機對應的域名,若爲多個,用空格隔開
38
39 #charset koi8-r; #設置網頁的編碼格式
40
41 #access_log logs/host.access.log main; #設置虛主機的訪問日誌
42
43 location / { #設置虛擬主機的基本信息
44 root html; #當前主機訪問網站根目錄
45 index index.html index.htm; #設置虛擬主機默認訪問的網頁
46 }
47
48 #error_page 404 /404.html; #定義錯誤提示頁面(404)
49
50 # redirect server error pages to the static page /50x.html
51 #
52 error_page 500 502 503 504 /50x.html; #定義50x錯誤提示頁面
53 location = /50x.html { #“=”號表示精確匹配
54 root html; #當前主機訪問網站根目錄
55 }
56
57 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
58 #
59 #location ~ \.php$ { #對php腳本(區分大小寫,以php結尾)啓用反向代理
60 # proxy_pass http://127.0.0.1;
61 #}
62
63 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
64 #
65 #location ~ \.php$ { #php腳本請求全部轉發到fastcgi處理,使用fastcgi默認配置
66 # root html; #網站根目錄
67 # fastcgi_pass 127.0.0.1:9000; #拋給本地的9000端口
68 # fastcgi_index index.php; #設定動態首頁
69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 # include fastcgi_params; #設定和fastcgi交互的相關參數包含文件
71 #}
72
73 # deny access to .htaccess files, if Apache's document root
74 # concurs with nginx's one
75 #
76 #location ~ /\.ht { #禁止訪問.htxx文件
77 # deny all;
78 #}
79 }
80
81
82 # another virtual host using mix of IP-, name-, and port-based configuration
83 #
84 #server { #配置另一臺虛擬主機
85 # listen 8000;
86 # listen somename:8080;
87 # server_name somename alias another.alias;
88
89 # location / {
90 # root html;
91 # index index.html index.htm;
92 # }
93 #}
94
95
96 # HTTPS server #配置https服務
97 #
98 #server {
99 # listen 443 ssl;
100 # server_name localhost;
101
102 # ssl_certificate cert.pem; #爲這個虛擬主機指定pem格式的證書文件
103 # ssl_certificate_key cert.key; #爲這個虛擬主機指定pem格式的密鑰
104
105 # ssl_session_cache shared:SSL:1m; #設置儲存SSL會話的緩存類型和大小
106 # ssl_session_timeout 5m; #設置客戶端能夠反覆使用儲存再緩存中的會話參數時間
107
108 # ssl_ciphers HIGH:!aNULL:!MD5; #指出允許的密碼(爲openssl支持的格式)
109 # ssl_prefer_server_ciphers on; #使用SSLV3和TLSV1協議的服務器密碼將優先於客戶端密碼
110
111 # location / {
112 # root html;
113 # index index.html index.htm;
114 # }
115 #}
116 }