王牌技能网站架构服务 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;
}