**************************学习nginx***************************
1. nginx配置分为4个区
1)全局区:
work_processes 1; #工作进程 1 一般设为 CPU数*核数(怎么看核数和CPU数?打开任务管理器-》性能-》可以看到内核数4)
2)event区:
work_connections 1024; #一个工作进程允许最大连接数1024
3)http区:
server块(配置虚拟主机):
配置一个虚拟主机,至少要包含(只是访问静态文件):
listen 80;
server_name localhost;#基于域名配置
location / {
root /home/wwwroot/default/test; # 当访问localhost域名的时候,映射(跳转)到那个目录下,必须要有这个配置
index index.php index.html index.htm;
}
4)root 配置根目录也可以使用相对路径,如果使用相对路径 ,那么相对的是nginx目录。以centos为例:相对于/usr/local/nginx。
意思就是和nginx在同一级目录下。
5)基于端口配置虚拟主机,本地配置,直接listen 端口号;但是呢,线上基于端口配置的时候,需要开放端口。或者使用nginx转发。
2. nginx 日志管理:
1)每个server块都可以分别生成一个日志。(如果你从来没有配置过日志,系统是有自带的日志的)
# access_log logs/host.access.log main; # 含义:访问日志存放于 logs/host.access.log ; 使用的格式:main (格式可以自定义)
2)main格式是什么?
日志格式是指 日志中要记录那些内容。
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
对这几项含义进行说明:
$remote_addr:远程IP $remote_user:远程用户 $time_local:用户时间 $request:请求方式 $status:状态码 $body_bytes_sent:请求体body长度
$http_referer:来源信息 $http_user_agent:用户代理/蜘蛛(可能会显示蜘蛛) $http_x_forwarded_for:被转发的请求的原始IP
$http_x_forwarded_for:在经过代理时,代理把你本来的IP加在此头信息中,传输你的原始IP。
至于还有哪些格式的选项,可以上网查一下。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
nginx配置虚拟主机(多提一句: 如果是虚拟机上配置域名, 想在宿主机上访问域名, 需要在宿主机上的host文件里解析域名)
server { root /home/wwwroot/default/bai; index index.php index.html index.htm; server_name yourservername; client_max_body_size 20m; #location / { #try_files $uri $uri/ =404; #} #error_page 404 /404.html; #error_page 500 502 503 504 /50x.html; #location = /50x.html { # root /var/www/html; #} location = /favicon.ico { log_not_found off; access_log off; } location ~ \.php$ { #try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
- 查看nginx的启动用户 (nginx.conf里user对应的用户要和启动用户一致? 但是我的nginx.conf里的user是www-data, 而查看的是bneglect, 感觉不是这么回事, 有时间研究研究)
ps aux | grep "nginx: worker process" | awk '{print $1}' #注意命令之间空格不要落下
- 报403,可能是因为没有索引 , 看看是否加上 index index.php index.html 等