Nginx("engine x")是一款是由俄羅斯的程序設計師Igor Sysoev所開發高性能的 Web和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。
在高連接併發的情況下,Nginx是Apache服務器不錯的替代品。
一、查看軟件目錄結構
1. 日誌文件切割
a. 利用專用文件切割
[root@web02 ~]# cat /etc/logrotate.conf
# rotate log files weekly
weekly --- 定義默認日誌切割的週期
# keep 4 weeks worth of backlogs
rotate 4 --- 定義只保留幾個切割後的文件
# create new (empty) log files after rotating old ones
create --- 創建出一個相同的源文件
# use date as a suffix of the rotated file
dateext --- 定義角標(擴展名稱信息)
# uncomment this if you want your log files compressed
#compress --- 是否對切割後的文件進行壓縮處理
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d --- 加載包含/etc/logrotate.d/目錄中文件配置
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp { --- 單獨對某個文件進行切割配置
monthly
create 0664 root utmp
minsize 1M --- 最小大小爲1M,小於1M不進行切割
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
b. 利用腳本實現切割
[root@web02 ~]# vim /etc/access_log.sh
#!/bin/bash
mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%F).log
[root@web02 ~]# systemctl restart nginx
2. 服務配置文件
[root@web02 ~]# cat /etc/nginx/nginx.conf
第一個部分: 配置文件主區域配置
user www; --- 定義worker進程管理的用戶
worker_processes 2; ---定義有幾個worker進程 == CPU核數 / 核數的2倍
error_log /var/log/nginx/error.log warn; --- 定義錯誤日誌路徑信息
pid /var/run/nginx.pid; --- 定義pid文件路徑信息
第二個部分: 配置文件事件區域
events {
worker_connections 1024; --- 一個worker進程可以同時接收1024訪問請求
}
第三個部分: 配置http區域
http {
include /etc/nginx/mime.types; --- 加載一個配置文件
default_type application/octet-stream; --- 指定默認識別文件類型
log_format oldboy '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
--- 定義日誌的格式
access_log /var/log/nginx/access.log oldboy;
--- 指定日誌路徑
sendfile on; ---用來提高服務器性能
#tcp_nopush on;
keepalive_timeout 65; --- 超時時間
#gzip on; ---是否需要開啓壓縮傳輸
include /etc/nginx/conf.d/*.conf; --- 加載一個配置文件
}
/etc/nginx/nginx.d/default --- 擴展配置(虛擬主機配置文件)
第四個部分: server區域信息(配置一個網站 www/bbs/blog -- 一個虛擬主機)
server {
listen 8080; --- 指定監聽的端口
server_name www.oldboy.com; --- 指定網站域名
root /usr/share/nginx/html; --- 定義站點目錄的位置
index index.html index.htm; --- 定義首頁文件
error_page 500 502 503 504 /50x.html; --- 優雅顯示頁面信息
location = /50x.html {
root /usr/share/nginx/html;
}
}
二、Nginx服務應用
1. 搭建一個網站
a.編寫虛擬主機配置文件
[root@web02 ~]# cd /etc/nginx/conf.d/
[root@web02 conf.d]# vim www.conf
server {
listen 80;
server_name www.southbay.com;
location / {
root /html/www;
index index.html;
}
}
b. 獲取開發人員編寫的網站代碼
[root@web02 ~]# cd /html/www
[root@web02 www]# vim index.html
<html>
<meta charset="utf-8">
<head>
<title>by southbay</title>
</head>
<body>
southbay study file
<table border=1>
<tr> <td>01</td> <td>123456</td> </tr>
<tr> <td>02</td> <td>234567</td> </tr>
<tr> <td>03</td> <td>345678</td> </tr>
</table>
<a href="http://blog.southbay.com">
<img src="southbay.jpg" />
</a>
</body>
</html>
c.重啓nginx服務(平滑重啓)
[root@web02 ~]# systemctl reload nginx
[root@web02 ~]# nginx -s reload
2. 一臺主機搭建多個網站
[root@web02 conf.d]# vim bbs.conf
server {
listen 80;
server_name bbs.southbay.com;
location / {
root /html/bbs;
index index.html;
}
}
[root@web02 conf.d]# vim blog.conf
server {
listen 80;
server_name blog.southbay.com;
location / {
root /html/blog;
index index.html;
}
}
[root@web02 conf.d]# vim www.conf
server {
listen 80;
server_name www.southbay.com;
location / {
root /html/www;
index index.html;
}
}
3. 虛擬主機訪問方式
# 基於域名的方式進行訪問
server {
listen 80;
server_name www.southbay.com;
location / {
root /html/www;
index index.html;
}
}
# 基於地址的方式進行訪問
server {
listen 10.0.0.7:80;
server_name www.southbay.com;
location / {
root /html/www;
index index.html;
}
}
# 基於端口的方式進行訪問
server {
listen 8080;
server_name www.southbay.com;
location / {
root /html/www;
index index.html;
}
}
4. 配置文件別名功能
server {
listen 80;
server_name www.southbay.com southbay.com;
location / {
root /html/www;
index index.html;
}
}
三、Nginx常用模塊
1. 安全訪問配置(ngx_http_access_module)
Syntax:deny address | CIDR | unix: | all;
Default: —
Context:http, server, location, limit_except
[root@web01 conf.d]# vim www.conf
server {
listen 80;
server_name www.southbay.com;
location / {
root /html/www;
index index.html;
}
location /southbay {
deny 10.0.0.0/24;
allow 172.16.1.0/24;
root /html/www;
index index.html;
}
}
2. 用戶訪問認證配置(ngx_http_auth_basic_module)
Syntax:auth_basic
string
| off
;
Default:auth_basic off;
Context:http
, server
, location
, limit_except
server {
listen 80;
server_name www.southbay.com;
location / {
root /html/www;
index index.html;
auth_basic "southbay";
auth_basic_user_file password/htpasswd;
}
3. 網站文件共享服務器(ngx_http_autoindex_module)
Syntax:autoindex on | off;
Default:autoindex off;
Context:http, server, location
server {
listen 80;
server_name www.southbay.com;
location / {
root /html/www;
autoindex on; --- 開啓nginx站點目錄索引功能
}
說明:1. 需要將首頁文件進行刪除
2. mime.types媒體資源類型文件作用
文件中有的擴展名信息資源, 進行訪問時會直接看到數據信息
文件中沒有的擴展名信息資源, 進行訪問時會直接下載資源
4. 網站狀態監控(ngx_http_stub_status_module)
Syntax:stub_status;
Default:—
Context:server
, location
server {
listen 80;
server_name state.southbay.com
stub_status;
}
5. 日誌功能配置
a. 訪問日誌(ngx_http_log_module)
Syntax:access_log
path
[format
[buffer
=size
] [gzip[=
] [level
]flush
=time
] [if
=condition
]]; access_log
off
;
Default:access_log logs/access.log combined;
Context:http
, server
, location
, if in location
, limit_except
[root@web02 ~]# cat /etc/nginx/nginx.conf
# 定義日誌內容格式
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 /var/log/nginx/access.log main;
常用參數 | 參數說明 |
---|---|
$remote_addr | 顯示用戶訪問源IP地址信息 |
$remote_user | 顯示認證的用戶名信息 |
$time_local | 顯示訪問網站時間 |
$request | 請求報文的請求行信息 |
$status | 用戶訪問網站狀態碼信息 |
$body_bytes_sent | 顯示響應的數據尺寸信息 |
$http_referer | 記錄調用網站資源的連接地址信息(防止用戶盜鏈) |
$http_user_agent | 記錄用戶使用什麼客戶端軟件進行訪問頁面的 (谷歌 火狐 IE 安卓 iphone) |
$http_x_forwarded_for | 表示 HTTP 請求端真實 IP |
b. 錯誤日誌(Core functionality)
Syntax:error_log
file
[level
];
Default:error_log logs/error.log error;
Context:main
, http
, mail
, stream
, server
, location
[root@web02 ~]# cat /etc/nginx/nginx.conf
# 錯誤日誌配置
error_log /var/log/nginx/error.log warn;
錯誤級別 | 錯誤級別說明 |
---|---|
debug | 調試級別, 服務運行的狀態信息和錯誤信息詳細顯示 |
info | 信息級別, 只顯示重要的運行信息和錯誤信息 |
notice | 通知級別: 更加重要的信息進行通知說明 |
warn | 警告級別: 可能出現了一些錯誤信息,但不影響服務運行 |
error | 錯誤級別: 服務運行已經出現了錯誤,需要進行糾正 |
crit | 嚴重級別: 必須進行修改調整 |
alert | 嚴重警告級別: 即警告,而且必須進行錯誤修改 |
emerg | 災難級別: 服務已經不能正常運行 |
6. location說明(ngx_http_core_module)
Syntax:location [
=
| ~
| ~*
| ^~
] uri
{ ... } location
@
name
{ ... }
Default:—
Context:server
, location
# 錯誤頁面優雅顯示
# 第一種方式:
location / {
root /html/www;
error_page 404 /error.jpg;
}
# 第二種方式:
error_page 500 501 502 503 504 /50x.html;
location =/50x.html {
root html;
}
# 第三種方式:
error_page 500 501 502 503 504 http://www.southbay.com/error.jpg;
# Location 符號說明
location = / { --- 精確匹配 優先級01
[ configuration A ]
}
location / { --- 默認匹配 優先級04
[ configuration B ]
}
location /documents/ { --- 按照目錄進行匹配 優先級03
[ configuration C ]
}
location ^~ /images/ { --- 優先匹配/不識別uri信息中符號信息 優先級02
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ { --- 不區分大小寫進行匹配 優先級03
[ configuration E ]
}
7. 頁面跳轉功能(http_rewrite_module)
Syntax:rewrite regex replacement [flag];
Default:—
Context:server, location, if
#第一種方式
server {
server_name southbay.com;
rewrite ^/(.*) http://www.southbay.com/$1 permanent;
}
#第二種方式
if ($host ~* "^southbay.com$") {
rewrite ^/(.*) http://www.southbay.com/$1 permanent;
}
標記 | 說明 |
---|---|
last | 本條規則匹配完成後,繼續向下匹配新的Loation URI規則 |
break | 本條規則匹配完成即終止,不在匹配後面任何規則 |
redirect | 返回302臨時重定向 |
permanent | 返回301永久重定向 |
四、Nginx常見錯誤
錯誤代碼 | 處理方式 |
---|---|
403 | 1. 不要禁止訪問 2. 因爲沒有首頁文件 |
404 |
1. 修改nginx配置文件---location 2. 在站點目錄中創建相應目錄或文件數據信息 |
413 |
修改nginx配置文件 client_max_body_size 50m; --- 指定用戶上傳數據的大小限制(默認1M) 修改php.ini配置文件 upload_max_filesize = 50M --- 使PHP接收用戶上傳的更大的數據(默認2M) |
中文出現亂碼 | 網站配置文件加入 charset utf-8; |