#nginx子配置檔案目錄
conf.d
#php代理檔案
fastcgi_params
#字元集
koi-utf
koi-win
win-utf
#檔案的類型
mime.types
#主配置檔案
nginx.conf
[[email protected] nginx]# cat /etc/nginx/nginx.conf
#nginx 的使用者
user www;
#工作程序數
worker_processes 20;
#錯誤日志
error_log /var/log/nginx/error.log warn;
#pid檔案
pid /var/run/nginx.pid;
#事件驅動子產品
events {
#工作程序最大連接配接數
worker_connections 1000;
}
#http 核心子產品
http {
#包含的檔案類型
include /etc/nginx/mime.types;
#nginx預設是下載下傳所有檔案類型的,直接在浏覽器通路
default_type application/octet-stream;
#日志格式 名字 abc
log_format abc '$remote_addr - - - - zls qiudao $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format abd '$remote_addr - $remote_user "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#日志路徑 調用abc格式
access_log /var/log/nginx/access.log abc;
#高效傳輸
sendfile on;
#tcp_nopush on;
#長連接配接逾時 65s
keepalive_timeout 65;
#開啟壓縮
#gzip on;
#包含子配置檔案目錄
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/*.conf;
}
#虛拟主機
server {
#監聽的端口
listen 80;
#域名,_; localhost;
server_name game.abc.com;
#location 比對規則 就是location 比對到根 就到根裡找
location / {
#指定站點目錄
root /code/h5_games;
#指定索引頁面(預設首頁)
index index.html;
}
}
日志格式
$remote_addr # 記錄用戶端IP位址
$remote_user # 記錄用戶端使用者名
$time_local # 記錄通用的本地時間
$time_iso8601 # 記錄ISO8601标準格式下的本地時間
$request # 記錄請求的方法以及請求的http協定
$status # 記錄請求狀态碼(用于定位錯誤資訊)
$body_bytes_sent # 發送給用戶端的資源位元組數,不包括響應頭的大小
$bytes_sent # 發送給用戶端的總位元組數
$msec # 日志寫入時間。機關為秒,精度是毫秒。
$http_referer # 記錄從哪個頁面連結通路過來的 就是浏覽器,像360浏覽器
$http_user_agent # 記錄用戶端浏覽器相關資訊
$http_x_forwarded_for #記錄用戶端IP位址
$request_length # 請求的長度(包括請求行, 請求頭和請求正文)。
$request_time # 請求花費的時間,機關為秒,精度毫秒
# 注:如果Nginx位于負載均衡器,nginx反向代理之後, web伺服器無法直接擷取到客 戶端真實的IP位址。
# $remote_addr擷取的是反向代理的IP位址。 反向代理伺服器在轉發請求的http頭資訊中,
# 增加X-Forwarded-For資訊,用來記錄用戶端IP位址和用戶端請求的伺服器位址。
log_format abd '$remote_addr - |$remote_user| [$time_local] "$request" '
'$status $body_bytes_sent $bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
game日志記錄實戰
location /favicon.ico {
access_log off;
return 200;
}
location /js/common.js {
access_log /var/log/nginx/js.log abc;
}
日志切割
/var/log/nginx/*.log {
#按天切割
daily
#忽略丢失的日志
missingok
#保留52天的日志
rotate 52
#檔案壓縮
compress
#延時壓縮
delaycompress
#不切割控日志
notifempty
#建立出來新日志的檔案權限 640 屬主(nginx)和屬組(adm)
create 640 nginx adm
#所有檔案歸檔完成後執行腳本
sharedscripts
#執行的指令
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
[[email protected] conf.d]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily # 每天切割日志
missingok # 日志丢失忽略
rotate 52 # 日志保留52天
compress # 日志檔案壓縮
delaycompress # 延遲壓縮日志
notifempty # 不切割空檔案
create 640 nginx adm # 日志檔案權限
sharedscripts #所有檔案歸檔完成後執行腳本
postrotate # 切割日志執行的指令
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
nginx常用子產品
1)目錄索引子產品
ngx_http_autoindex_module子產品處理以斜杠字元('/')結尾的請求,并生成目錄清單。
當ngx_http_index_module子產品找不到索引檔案時,通常會将請求傳遞給ngx_http_autoindex_module子產品。
Nginx
預設是不允許列出整個目錄浏覽下載下傳。
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
# autoindex常用參數
autoindex_exact_size off;
預設為on, 顯示出檔案的确切大小,機關是bytes。
修改為off,顯示出檔案的大概大小,機關是kB或者MB或者GB。
autoindex_localtime on;
預設為off,顯示的檔案時間為GMT時間。
修改為on, 顯示的檔案時間為檔案的伺服器時間。
charset utf-8,gbk;
預設中文目錄亂碼,添加上解決亂碼。
配置示例:
[[email protected] ~]# vim /etc/nginx/conf.d/module.conf
server {
listen 80;
server_name module.driverzeng.com;
charset utf-8,gbk;
localtion / {
root /code;
index index.html index.htm;
}
location /download {
alias /module;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
2)Nginx狀态子產品
ngx_http_stub_status_module
子產品提供對基本狀态資訊的通路。 預設情況下不建構此子產品,應使用
--with-http_stub_status_module
配置參數啟用它
Syntax: stub_status;
Default: —
Context: server, location
配置
Nginx status
示例
server {
listen 80;
server_name module.driverzeng.com;
access_log off;
location /nginx_status {
stub_status;
}
}
server {
listen 80;
server_name module.driverzeng.com;
charset utf-8,gbk;
localtion / {
root /code;
index index.html index.htm;
}
location /download {
alias /module;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
location /nginx_status {
stub_status;
}
}
打開浏覽器通路:http://module.driverzeng.com/nginx_status
Active connections: 2
server accepts handled requests
373 373 695
Reading: 0 Writing: 1 Waiting: 1
Active connections # 目前活動的連接配接數
accepts # 目前的總連接配接數TCP
handled # 成功的連接配接數TCP
requests # 總的http請求數
Reading # 請求
Writing # 響應
Waiting # 等待的請求數,開啟了keepalive
# 注意, 一次TCP的連接配接,可以發起多次http的請求, 如下參數可配置進行驗證
keepalive_timeout 0; # 類似于關閉長連接配接
keepalive_timeout 65; # 65s沒有活動則斷開連接配接
3)Nginx通路控制
#允許配置文法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
#拒絕配置文法
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
1)通路控制配置示例,拒絕指定的IP,其他全部允許
server {
listen 80;
server_name module.driverzeng.com;
access_log off;
location /nginx_status {
stub_status;
deny 10.0.0.1;
allow all;
}
}
2) 通路控制配置示例, 隻允許誰能通路, 其它全部拒絕
server {
listen 80;
server_name module.driverzeng.com;
access_log off;
location /nginx_status {
stub_status;
allow 10.0.0.0/24;
allow 127.0.0.1;
deny all;
}
}
4)登入認證子產品
1)基于使用者登陸認證配置文法
#通路提示字元串
Syntax: auth_basic string| off;
Default: auth_basic off;
Context: http, server, location, limit_except
#賬戶密碼檔案
Syntax: auth_basic_user_file file;
Default: -
Context: http, server, location, limit_except
2)基于使用者登陸認證配置實踐
#1.需要安裝httpd-tools,該包中攜帶了htpasswd指令
[[email protected] ~]# yum install httpd-tools
#2.建立新的密碼檔案, -c建立新檔案 -b允許指令行輸入密碼
[[email protected] ~]# htpasswd -b -c /etc/nginx/auth_conf zls zls
#3.nginx配置調用
server {
listen 80;
server_name module.driverzeng.com;
access_log off;
location /nginx_status {
stub_status;
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file auth_conf;
}
}
game記錄日志
[[email protected] conf.d]# vim game.conf
server {
listen 80;
server_name game.abc.com;
access_log /var/log/nginx/game.abc.com_access.log main;
location / {
root /code/h5_games;
index index.html;
}
location /favicon.ico {
access_log off;
return 200;
}
location /js/common.js {
access_log /var/log/nginx/js.log main;
}
}
[root@web01 conf.d]# tail -f /var/log/nginx/game.abc.com_access.log
添加日志目錄索引子產品
[[email protected] conf.d]# vim autoindex.conf
server {
listen 80;
server_name dir.abc.com;
autoindex on;
location / {
root /dir;
}
}
[[email protected] conf.d]# mkdir /dir
[[email protected] conf.d]# chown -R www.www /dir
[[email protected] conf.d]# cd /dir
[[email protected] dir]# ll
total 0
[[email protected] dir]# chown -R www.www /dir
[[email protected] dir]# nginx -t
[[email protected] dir]# nginx -s reload
然後再域名解析那裡寫 dir.abc.com就可以通路了
接下來模仿阿裡雲的鏡像
[[email protected] dir]# mkdir centos/7/os -p
[[email protected] dir]# chown -R www.www /dir
然後通路一下就會發現和阿裡雲的一模一樣
[[email protected] dir]# cd centos/7/os/
[[email protected] os]# ll
total 0
[[email protected] os]# rz 上傳一個檔案
[[email protected] os]# vim /etc/nginx/conf.d/autoindex.conf
server {
listen 80;
server_name dir.abc.com;
charset utf-8,gbk;
autoindex on;
location / {
root /dir;
autoindex_exact_size :off;
autoindex_localtime on;
}
}
~
狀态子產品
[[email protected] conf.d]# gzip autoindex.conf
[[email protected] conf.d]# vim game.conf
t
然後通路game.abc.com/download/ 就會出現下面的界面
狀态
[[email protected] conf.d]# vim game.conf
server {
listen 80;
server_name game.abc.com;
access_log /var/log/nginx/game.abc.com_access.log main;
location / {
root /code/h5_games;
index index.html;
}
location /download {
alias /dir;
autoindex on;
}
location /status {
stub_status;
}
}
[[email protected] conf.d]# nginx -t
[[email protected] conf.d]# nginx -s reload
通路http://game.abc.com/status
會顯示
通路控制子產品
[[email protected] conf.d]# yum install -y httpd-tools
[[email protected] conf.d]# htpasswd -b -c /code/aa zhp 123
Adding password for user zhp
[[email protected] conf.d]# ll /code/ 發現下面有一個aa
total 8
-rw-r--r-- 1 root root 42 Aug 15 19:07 aa
drwxr-xr-x 6 root root 108 May 3 14:11 h5_games
-rw-r--r-- 1 www www 5 Aug 14 23:21 index.html
[[email protected] conf.d]# cat /code/aa 檢視一下,發現是使用者名,然後密碼是加密的
zhp:$apr1$r6b8yFQ9$Y1oB/C91Gijxi1ji7mLhO1
[[email protected] conf.d]# vim /etc/nginx/conf.d/auth.conf
server {
listen 80;
server_name dir.abc.com;
location /{
root /dir;
index index.html;
auth_basic "abc";
auth_basic_user_file /code/aa;
}
}
~
~
[[email protected] conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] conf.d]# nginx -s reload
[[email protected] conf.d]# cd /dir/
[[email protected] dir]# ll
total 0
drwxr-xr-x 3 www www 15 Aug 15 16:10 centos
[[email protected] dir]# echo auth > index.html
[[email protected] dir]# ll
total 4
drwxr-xr-x 3 www www 15 Aug 15 16:10 centos
-rw-r--r-- 1 root root 5 Aug 15 19:17 index.html
[[email protected] dir]# chown -R www.www /dir
通路dir.abc.com 會讓你輸入密碼,和使用者
[[email protected] conf.d]# vim game.conf
[[email protected] conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] conf.d]# nginx -s reload
server {
listen 80;
server_name game.abc.com;
set $domain "http://www.abc.com"; 設定一個限制 通路www.abc.com 就報404的錯
if ($http_referer != $domain){
return 404;
}
location / {
root /dir;
index index.html;
}
}
轉載于:https://www.cnblogs.com/223zhp/p/11370461.html