王牌技能網站架構服務 Nginx javascript
知識點說明:php
測試頁面: http://localhost/index.htmlcss
目錄:html
/etc/nginx/nginx.conf #Nginx主配置文件java
/usr/bin/nginx #命令文件node
/var/log/nginx/access.log #常規日誌存儲nginx
/var/log/nginx/error.log #錯誤日誌存儲git
/usr/share/nginx/html #站點目錄、網站的根目錄sql
/etc/nginx/conf.d #子配置目錄vim
/etc/nginx/mime.types #媒體類型 (http協議中的文件類型)
/etc/logrotate.d/nginx #nginx日誌切割的配置文件
(/var/log/nginx/access.log)日誌格式說明:
$remote_addr #記錄客戶端 IP 地址
$remote_user #記錄客戶端用戶名
[$time_local] #記錄通用的本地時間
"$request" #記錄請求的方法以及請求的 http 協議
$status #記錄請求狀態碼(用於定位錯誤信息)
$body_bytes_sent #發送給客戶端的資源字節數,不包括響應頭的大小
$http_referer #記錄從哪一個頁面連接訪問過來的(防止盜鏈)
$http_user_agent #記錄客戶端瀏覽器相關信息
$http_x_forwarded_for #負載均衡 記錄客戶端 IP 地址
(/var/log/nginx/error.log) 錯誤級別:
debug :調試級別,服務運行的狀態信息和錯誤信息詳細顯示 (信息越多)
info :信息級別,只顯示重要的運行信息和錯誤信息
notice:通知級別:更加劇要的信息進行通知
warn :警告級別:可能出現了一些錯誤信息,但不影響服務容許
error :錯誤級別:服務運行已經出現了錯誤,須要進行糾正 (推薦使用)
crit :嚴重級別:必須進行修改調整
alert :嚴重警告級別:即警告,並且必須進行錯誤修改
emerg :災難級別:服務已經不能正常運行 (信息越少)
命令總結:
systemctl reload nginx #平滑重啓 (沒有修改Nginx.conf 配置文件能夠用)
systemctl restart nginx #重啓服務 (沒有修改Nginx.conf 配置文件能夠用)
nginx -t # 檢查語句是否正常
ngxin -s # 啓動或重啓
使用說明 (一)安裝:
yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel nginx # 下載 依賴包 與 Nginx
systemctl start nginx && systemctl enable nginx # 開啓Nginx 以及 加入啓動項
使用說明 (二)配置 nginx.conf:
groupadd www -g 666 # 建立組爲www uuid爲666
useradd www -s /sbin/nologin -M -u 666 -g 666 # 建立用戶名 www 爲指定目錄 /sbin/nologin uuid和guid 爲 666
第一個部分:配置文件區域配置
user www www; # 用戶名
worker_processes auto; # CPU親和力
error_log /var/log/nginx/error.log warn; # 錯誤日誌
pid /run/nginx.pid; # 錯誤日誌定義等級,[ debug | info | notice | warn | error | crit ]
include /usr/share/nginx/modules/*.conf; # 負載動態模塊
include /etc/nginx/default.d/*.conf; # 類型模塊
################# 作過性能調優 ##################
worker_rlimit_nofile 65535; # worker進程最大打開文件數
第二個部分:配置文件事件區域
events {
################# 作過性能調優 ##################
use epoll; # 使用epoll模型
worker_connections 65535; # 單個進程容許的客戶端最大鏈接數
multi_accept on; # 儘量多的接受請求
accept_mutex on; # 不要隨便關閉
}
第三個部分:配置http區域
http {
include /etc/nginx/conf.d/*.conf; # 虛擬主機配置文件
include /etc/nginx/mime.types; # 定mime類型,類型由mime.type文件定義
default_type application/octet-stream; # 加載默認識別文件類型
access_log /var/log/nginx/access.log main buffer=32k;1
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 定義日誌的格式
################# 作過性能調優(Nginx與PHP之間FastCGI) ##################
# 時間超時設定
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
# 緩衝/緩存設置
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_path /var/run/nginx tmp cache;
fastcgi_temp_file_write_size 128k;
fastcgi_cache_path /var/run/nginxcache levels=1:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; # 緩存目錄
################# 作過性能調優 ##################
sendfile on; # 開啓高效文件傳輸模式
tcp_nopush on; # 防止網絡阻塞
tcp_nodelay on; # 提升I/O性能
keepalive_timeout 65; # Keepalive 超時時間
client_header_timeout 15; # 讀取客戶端請求頭數據的超時時間 默認60秒
client_body_timeout 15; # 讀取客戶端請求主體的超時時間 默認60秒
send_timeout 15; # 響應客戶端的超時時間 默認60秒
client_max_body_size 10m; # 上傳文件的大小限制 默認1m
types_hash_max_size 2048;
limit_conn_zone $binary_remote_addr zone=addr:10m;
######################## 服務器內存緩存 ##################################
open_file_cache max=2000 inactive=20s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors off;
######################## gzip壓縮 ##################################
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 6;
gzip_types text/plain application/x javascript text/css application/xml text/javascript application/x httpd php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
gzip_http_version 1.1;
################# 作過安全調優 ##################
server_tokens off ; # 隱藏版本號
autoindex off ; # 隱藏目錄
}
安全一 限制惡意流量訪問(模塊:ngx_http_limit_req_module):
安全二 頁面禁止用戶訪問(模塊:ngx_http_access_module) :deny (阻止) allow(容許)
vim /usr/local/nginx/conf.d/www.conf
server {
listen 172.16.55.3:8080 default_server ; # 優先級
server_name www.oldboy.com blog.oldboy.com; # 指定網站域名
error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 /404.html;
location = /404.html {
root /root/learning_system_back_v2.0/public;
}
################# 作過安全調優 ##################
location / {
root html/www;
index index.php index.html index.htm;
access_log /var/log/nginx/access.log main; # 訪問日誌
}
################# PHP調優 ##################
location ~ .*\.(php|php5)?$ {
root /html/www; #定義站點目錄
limit_conn addr 1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/wugk$fastcgi_script_name;
include fastcgi.conf;
include fastcgi_params; 變量配置文件
# FastCGI調優
fastcgi_cache ngx_fcgi_cache;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
}
################# Expires緩存調優 ##############
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /;
expires 7d;
}
################# 查看Nginx狀態(ngx_http_stub_status_module)##############
location /status {
stub_status on;
}
################# 安全調優 ##############
location ~*\.(sh|git|bak|sql|old)$ {
return 403;
}
}
}
########################## 項目類型 ##########################################
安全一 頁面用戶認證訪問(ngx_http_auth_basic_module):
yum -y install httpd #須要Apache支持
mkdir /etc/nginx/password #建立password建立存儲密碼
chmod 600 /etc/nginx/password #放權限
htpasswd - (密碼加密 ) 參數:-bc(免交互+建立密碼+不顯示文件內容) 舉例: htpasswd -bcn /etc/nginx/password/htpasswd oldboy oldboy 123456789
rm -f htpasswd #刪除密碼
server {
listen 80 ; ————監聽端口
server_name www.oldboy.com ; ————指定網站域名
location / {
root /usr/share/nginx/html; ————定義站點目錄
index index.html index.html;
auth_bascic "closed site"; —————開啓認證功能
auth_basic_user_file /etc/nginx/password/httpasswd; ———— 指定密碼文件
}
}
項目一 目錄索引(能夠加入 安全二,加入用戶認證):
ngx_http_autoindex_module ———— 索引功能
小技巧:
text/plain txt 改爲 php 能夠點擊不下載
vim /etc/nginx/mime.types
mkdir /etc/nginx/html/www/Centos{6.9,7.6}
server {
list 80 ;
server_name www.XX.com XX.com; ————別名功能
access_log /var/log/nginx/www_access.log main;
location / {
root /html/www; ————目錄
auth_basic "oldboy-sz-01"; ————用戶名
auth_bashic_user_file password/htpasswd; ————密碼
autoindex on; ————開啓索引功能
autoindex_localtime on; ————顯示的文件時間爲文件的服務器時間。
autoindex_exact_size off; ————顯示出文件的大概大小
chartset utf-8; ————設置中文不碼
}
}
項目二 網站跳轉(Nginx):
http_rewrite_module
跳轉方式:
永久跳轉 : permanent 301 會將跳轉信息進項緩存
臨時跳轉 : redirect 302 不會緩存跳轉信息
server {
listen 80;
server_name oldboy.com;
if ($host ~*"^oldboy.com$"){
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
server {
list 80 ;
server_name www.oldboy.com; ————別名功能
stub_status; ————監控網站
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}