nginx配置详解
大家好,我是酷酷的韩~
下面分享一下nginx的配置文件:
一.nginx.conf文件
1.目录
/etc/nginx/nginx.conf
2.nginx.conf文件如下
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
3.nginx.conf文件详解,针对第二条
(1)user:设计nginx服务的系统使用用户
(2)worker_processes 工作进程数(设置成和cpu个数一致)
(3)error_log /var/log/nginx/error.log warn; 错误日志
(4)pid /var/run/nginx.pid; 服务启动时候pid
(5)events: worker_connections (每个进程允许的最大连接数,默认1024)
(6)events: use 工作进程数
(7)nginx默认的log_format记录哪些内容。
remote_addr:表示客户端地址
remote_user:HTTP用户端 客户请求nginx认证的用户名
time_local:时间
request:请求:get/Post http的协议版本
status:返回的状态
body_bytes_sent:从客户端响应给客户端 body信息
http_referer 上一级页面的http地址
http_user_agent:头信息内容,表示客户端用什么来访问的
http_x_forwarded_for:记录每一级请求http信息
(8)检查nginx配置文件格式是否正确
nginx -t /etc/nginx/nginx.conf
(9)检查nginx配置文件路径是否正确
nginx -c /etc/nginx/nginx.conf
二.default.conf文件
在niginx中 include /etc/nginx/conf.d/*.conf; 包含了default.conf文件
1.default.conf文件如下
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
2.部分配置详解
(1)listen 监听端口 默认80
nginx服务启动后直接输入ip即可
(2)启动页位置
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
可以修改其/usr/share/nginx/html/index.html 再去访问启动页,发现内容改变(下图加了"(ku)")
(2)错误页面
error_page 跟错误类型 错误页面
如 error_page 404 /404.html 表示找不到则跳转404.html页面
后续会持续更新~
别人能做到的事,自己也可以做到。-----酷酷的韩