nginx从入门到自闭
nginx安装卸载(CentOS:yum安装)
安装
yum install nginx
卸载
yum remove nginx
启动nginx
start nginx
指定配置文件启动nginx
start nginx -c filename
关闭nginx,完整有序的停止nginx,保存相关信息
nginx -s quit
关闭nginx,快速停止nginx,可能并不保存相关信息
nginx -s stop
重新载入nginx,当配置信息修改需要重新加载配置是使用
nginx -s reload
重新启动nginx(重新打开日志文件)
nginx -s reopen
测试nginx配置文件是否正确
nginx -t -c filename
nginx代理前端文件;
server {
listen 8080;#ssl;
root html;
# server_name bee.youxinsign.com yoc.tech;#服务名称
index index.html index.htm;
location / {
#代理目录
root /home/admin;
index index.html;
}
location /upload/ {
#代理文件管理目录
alias /home/admin/upload/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ /\.ht {
deny all;
}z
error_page 497 301 https://$http_host$request_uri;
}
注:
(1) . alias虚拟目录配置中,location匹配的path目录如果后面不带"/",那么访问的url地址中这个path目录后面加不加"/“不影响访问,访问时它会自动加上”/";
但是如果location匹配的path目录后面加上"/",那么访问的url地址中这个path目录必须要加上"/",访问时它不会自动加上"/"。如果不加上"/",访问就会失败!
(2) . root目录配置中,location匹配的path目录后面带不带"/",都不会影响访问。
nginx代理后端文件
server {
listen 80;
root html;
index index.html index.htm;
location / {
#跨域处理 所有option请求都返回允许
if ($request_method = 'OPTIONS') {
return 200;
}
#为所有请求增加根链接
#rewrite ^(.*)$ /msie/$1 break;
#为所有请求删除某段链接
rewrite ^/api/(.*)$ /$1 break;
#转发请求
proxy_pass http://localhost:10080;
#跨域处理
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
#add_header Access-Control-Allow-Headers 'test';
#允许访问的organ
add_header Access-Control-Allow-Origin $http_origin;
#请求信息配置
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#连接超时时间
proxy_connect_timeout 90;
#发送超时时间
proxy_send_timeout 90;
#读取超时时间
proxy_read_timeout 90;
#请求包含内容最大大小
client_max_body_size 200m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ /\.ht {
deny all;
}
}
负载均衡
upstream myapp{
#least_conn; #将连接分配至连接最少的服务上
#ip_hash; #按访问ip hash绑定
#url_hash; #按访问地址hash绑定
# 权重,越高分的越多 热备 结合使用 最大失败2次,失败后该服务超时2秒
server localhost:10080 weight=1; #backup #max_fails=2 fail_timeout=2s;
server localhost:10081;
}
proxy_pass http://myapp;
nginx性能优化------>待补充
# nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数。
worker_processes 8;
#为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或者将一个进程分配到多个cpu。
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
# 作用于event的I/O多路复用模型
use epoll;
#收到新连接通知后接受尽可能多的连接,作用于event
multi_accept on;
nginx常用参数
$args #请求中的参数值
$query_string #同 $args
$arg_NAME #GET请求中NAME的值
$is_args #如果请求中有参数,值为"?",否则为空字符串
$uri #请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如"/foo/bar.html"。
$document_uri #同 $uri
$document_root #当前请求的文档根目录或别名
$host #优先级:HTTP请求行的主机名>"HOST"请求头字段>符合请求的服务器名
$hostname #主机名
$https #如果开启了SSL安全模式,值为"on",否则为空字符串。
$binary_remote_addr #客户端地址的二进制形式,固定长度为4个字节
$body_bytes_sent #传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的"%B"参数保持兼容
$bytes_sent #传输给客户端的字节数
$connection #TCP连接的序列号
$connection_requests #TCP连接当前的请求数量
$content_length #"Content-Length" 请求头字段
$content_type #"Content-Type" 请求头字段
$cookie_name #cookie名称
$limit_rate #用于设置响应的速度限制
$msec #当前的Unix时间戳
$nginx_version #nginx版本
$pid #工作进程的PID
$pipe #如果请求来自管道通信,值为"p",否则为"."
$proxy_protocol_addr #获取代理访问服务器的客户端地址,如果是直接访问,该值为空字符串
$realpath_root #当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径
$remote_addr #客户端地址
$remote_port #客户端端口
$remote_user #用于HTTP基础认证服务的用户名
$request #代表客户端的请求地址
$request_body #客户端的请求主体:此变量可在location中使用,将请求主体通过proxy_pass,fastcgi_pass,uwsgi_pass和scgi_pass传递给下一级的代理服务器
$request_body_file #将客户端请求主体保存在临时文件中。文件处理结束后,此文件需删除。如果需要之一开启此功能,需要设置client_body_in_file_only。如果将次文件传递给后端的代理服务器,需要禁用request body,即设置proxy_pass_request_body off,fastcgi_pass_request_body off,uwsgi_pass_request_body off,or scgi_pass_request_body off
$request_completion #如果请求成功,值为"OK",如果请求未完成或者请求不是一个范围请求的最后一部分,则为空
$request_filename #当前连接请求的文件路径,由root或alias指令与URI请求生成
$request_length #请求的长度 (包括请求的地址,http请求头和请求主体)
$request_method #HTTP请求方法,通常为"GET"或"POST"
$request_time #处理客户端请求使用的时间; 从读取客户端的第一个字节开始计时
$request_uri #这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI,不包含主机名,例如:"/cnphp/test.php?arg=freemouse"
$scheme #请求使用的Web协议,"http" 或 "https"
$server_addr #服务器端地址,需要注意的是:为了避免访问linux系统内核,应将ip地址提前设置在配置文件中
$server_name #服务器名
$server_port #服务器端口
$server_protocol #服务器的HTTP版本,通常为 "HTTP/1.0" 或 "HTTP/1.1"
$status #HTTP响应代码
$time_iso8601 #服务器时间的ISO 8610格式
$time_local #服务器时间(LOG Format 格式)
$cookie_NAME #客户端请求Header头中的cookie变量,前缀"$cookie_"加上cookie名称的变量,该变量的值即为cookie名称的值
$http_NAME #匹配任意请求头字段;变量名中的后半部分NAME可以替换成任意请求头字段,如在配置文件中需要获取http请求头:"Accept-Language",$http_accept_language即可
$http_cookie
$http_post
$http_referer
$http_user_agent
$http_x_forwarded_for
$sent_http_NAME #可以设置任意http响应头字段;变量名中的后半部分NAME可以替换成任意响应头字段,如需要设置响应头Content-length,$sent_http_content_length即可
$sent_http_cache_control
$sent_http_connection
$sent_http_content_type
$sent_http_keep_alive
$sent_http_last_modified
$sent_http_location
$sent_http_transfer_encoding