Nginx
systemctl start nginx #启动,restart-重启,stop-停止
ps -ef | grep -v grep | grep nginx
systemctl status nginx
nginx -v #查看版本
pkill -9 nginx
#fastcgi进程数
netstat -anpo | grep "php-cgi" | wc -l
接下来还要将其添加到开机项
systemctl enable nginx
#安装
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install -y nginx
用前缀字符串定义的location规则对URI进行匹配测试。
=号定义了精确的前缀字符串匹配,如果发现精确匹配则使用当前规则。否则继续下一步匹配。
匹配其它普通字符串,并存储最长匹配。如果匹配以^~开始的规则,则使用当前匹配,否则继续下一步匹配。
按顺序对URI进行正则规则匹配,发现匹配后停止并使用当前匹配。若所有正则都不匹配,则使用第3步存储的最长匹配规则。
~ 开头表示区分大小写的正则匹配;
~* 开头表示不区分大小写的正则匹配
整体匹配优先级 =精确匹配 > ^~前缀匹配 > 正则匹配 > 普通前缀字符串匹配
location / {
root html; #这是默认的目录是个文件夹
index index.html index.htm;
}
#user nobody;
worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; #开启rewrite日志 测试用 rewrite日志写在error_log里 rewrite_log on; error_log logs/error.log notice; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 128k; fastcgi_buffers 4 128k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; #gzip on; gzip on; gzip_min_length 1k; gzip_buffers 4 32k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; gzip_disable "MSIE [1-6]."; server_names_hash_bucket_size 128; client_max_body_size 100m; client_header_buffer_size 256k; large_client_header_buffers 4 256k; server { listen 80; server_name citest.com; index index.html index.php; root "d:/www/ci"; access_log logs/ciaccess.log main; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params;
#fastcgi_split_path_info ^(.+\.php)(/.*)$;
#fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
#fastcgi_connect_timeout 300;
#fastcgi_send_timeout 300;
#fastcgi_read_timeout 300;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 12h;
}
}
#include vhosts.conf;
}
![]()
119.90.20.111 - - [21/Oct/2017:14:44:32 +0800] "GET /images/templatemo_180_column_bg.jpg HTTP/1.1" 200 2140 "http://114.115.155.144/templatemo_style.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0" "-"
$remote_addr对应的是真实日志里的119.90.20.111,即客户端的IP。
$remote_user对应的是第二个中杠“-”,没有远程用户,所以用“-”填充。
[$time_local]对应的是[21/Oct/2017:14:44:32 +0800]。
“$request”对应的是”GET /images/templatemo_180_column_bg.jpg HTTP/1.1”。
$status对应的是200状态码,200表示正常访问。
$body_bytes_sent对应的是2140字节,即响应body的大小。
“$http_referer”对应的是”http://114.115.155.144/templatemo_style.css“,若是直接打开域名浏览的时,referer就会没有值,为”-“。
“$http_user_agent”对应的是”Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:56.0) Gecko/20100101 Firefox/56.0”。
“$http_x_forwarded_for”对应的是”-“,因为Web服务没有使用代理,因此此处为”-“。
access_log path [format [buffer=size [flush=time]] [if=condition]]; #为存放访问日志的缓冲区大小
access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; #为缓冲区的日志刷到磁盘的时间
access_log syslog:server=address[,parameter=value] [format [if=condition]]; #表示压缩级别
Nginx的日志格式与日志变量
Nginx跟Apache一样,都支持自定义输出日志格式,在进行Nginx日志格式定义前,先来了解一下关于多层代理获取用户真实IP的几个概念。
remote_addr:表示客户端地址,但有个条件,如果没有使用代理,这个地址就是客户端的真实IP,如果使用了代理,这个地址就是上层代理的IP。
X-Forwarded-For:简称XFF,这是一个HTTP扩展头,格式为 X-Forwarded-For: client, proxy1, proxy2,如果一个HTTP请求到达服务器之前,经过了三个代理 Proxy1、Proxy2、Proxy3,IP 分别为 IP1、IP2、IP3,用户真实IP为 IP0,那么按照 XFF标准,服务端最终会收到以下信息:
X-Forwarded-For: IP0, IP1, IP2
由此可知,IP3这个地址X-Forwarded-For并没有获取到,而remote_addr刚好获取的就是IP3的地址。
还要几个容易混淆的变量,这里也列出来做下说明:
$remote_addr:此变量如果走代理访问,那么将获取上层代理的IP,如果不走代理,那么就是客户端真实IP地址。
$http_x_forwarded_for:此变量获取的就是X-Forwarded-For的值。
$proxy_add_x_forwarded_for:此变量是$http_x_forwarded_for和$remote_addr两个变量之和。



浙公网安备 33010602011771号