获取全套nginx教程,请访问瓦力博客
为nginx主配置文件添加注释,更好的理解nginx
nginx的默认加载 /usr/share/nginx/html/ 文件
1.nginx主配置文件
打开 vim /etc/nginx/nginx.conf
user nginx; #设置nginx服务的系统使用用户
worker_processes 1; #工作进程数 默认设置和cpu相同就好了
error_log /var/log/nginx/error.log warn; #错误日志
pid /var/run/nginx.pid; #进程的pid
events {
worker_connections 1024; #每个进程允许最大连接数
}
http {
include /etc/nginx/mime.types; #设置http协议的Content-Type与扩展名对应关系
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 /var/log/nginx/access.log main; # 设置日志类型与log_format 对应
sendfile on; #零拷贝 nginx的一大优势,拷贝文件是不需要经过用户空间,直接在内核空间上拷贝
#tcp_nopush on;
keepalive_timeout 65; #连接超时 65s
#gzip on;
include /etc/nginx/conf.d/*.conf; #导入conf.d目录下的配置文件
}
小菜建议配服务的时候在/etc/nginx/conf.d/
目录下新建一个文件后缀为.conf
的文件
2.conf.d文件
在命令窗口输入
cd /etc/nginx/conf.d
进入到conf.d
目录下面有个默认default.conf
文件
server {
listen 80; #监听的端口号80 也可以这样写 192.168.1.140: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; #定义404的错误
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #定义 500 502 503 504错误
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;
#}
}