運維之網站的設定、虛擬主機使用
阿新 • • 發佈:2018-12-04
在conf中只有一個serve時,該站就是預設網站
基本設定
'''預設網站'''
server {
#虛擬主機使用的埠
listen 80;
#虛擬主機域名
server_name localhost;
#虛擬主機支援的字符集
charset utf-8;
#訪問日誌路徑,多個網站必須單獨設定
#access_log logs/host.access.log main;
#定義web根路徑
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;
}
訪問控制
某些連結只允許本機訪問
location /a {
allow 192.168.1.0/24; 或者 localhost
deny all; # 拒絕所有
#return 404; 可以迷惑訪問者
#return http://www.jd.com;
}
訪問頁面登入驗證
location /c { auth_basic "登陸驗證"; auth_basic_user_file /etc/nginx/htpasswd; }
自定義訪問日誌
- log_format格式變 :
- $remote_addr #記錄訪問 站的客戶端地址
- $remote_user #遠端客戶端 戶名
- $time_local #記錄訪問時間與時區
- $request # 戶的http請求起始 資訊
- $status #http狀態碼,記錄請求返回的狀態碼, 如:200、301、404等
- $body_bytes_sent #服務 傳送給客戶端的響應body位元組數
- $http_referer #記錄此次請求是從哪個連線訪問過來的,可以根據該引數進 防盜鏈設定。
- $http_user_agent #記錄客戶端訪問資訊, 如:瀏覽 、 機客戶端等
- $http_x_forwarded_for #當前端有代 服務 時,設定web節點記錄客戶端地址的配置,此引數 效的前提是代 服務 也要進 相關的x_forwarded_for設定
自定義日誌格式json
log_format main_json '{"@timestamp":"$time_local",' '"client_ip": "$remote_addr",'
'"request": "$request",'
'"status": "$status",'
'"bytes": "$body_bytes_sent",'
'"x_forwarded": "$http_x_forwarded_for",'
'"referer": "$http_referer"'
'}’;
access_log logs/access_json.log main_json;
防盜鏈
location /images/ {
alias /data/images/;
valid_referers none blocked *.ayitula.com;
if ($invalid_referer) {
return 403;
} }
虛擬主機
同時釋出兩個網站:
目錄:
DocumentRoot /usr/local/nginx/html/web1
DocumentRoot /usr/local/nginx/html/web2
- 基於IP的不同虛擬主機
!!缺點 需要多個IP 如果是公網IP 每個IP都需要付費
server {
listen location / {
root index
192.168.10.42:80;
html/web1;
index.html index.htm index.php;
}
}
# 第二個
server {
listen location / {
root index
192.168.10.52:80;
html/web2;
index.html index.htm;
}
}
- 基於埠的不同虛擬主機
!!缺點 缺點 埠你是無法告訴公網使用者 無法適用於公網客戶 適合內部使用者
# 基於埠
server {
listen 80;
#server_name www.abc.com;
location / {
root html/web1;
index index.html index.htm index.php;
}
}
server {
listen 8080;
#server_name www.abc.com;
location / {
root html/web2;
index index.html index.htm index.php;
}
}
- 基於域名的不同虛擬主機
!!缺點 一個網站必然有一個域名
server {
listen 80;
server_name www.abc.com;
location / {
root html/web2;
index index.html index.htm index.php;
}
}
server {
listen 80;
server_name www.xxjb.com;
location / {
root html/web2;
index index.html index.htm;
}
}