认识 nginx 的默认配置
cat /etc/nginx/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 '$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;
}
cat /etc/nginx/conf.d/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 /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;
#}
}
默认就是这两个配置文件,http 部分是关于 http 部分的配置,server 是关于服务器的配置,location 是关于路由的配置。
location 后面的 ~
表示路径使用正则匹配(区分大小写)。常用案例
location ~ .*\.(jpg|png|gif)$ {
gzip off;#关闭压缩
root /data/www/images;
}
location ~ .*\.(html|js|css)$ {
gzip on; #启用压缩
gzip_min_length 1k; #只压缩超过 1K 的文件
gzip_http_version 1.1; #启用 gzip 压缩所需的 HTTP 最低版本
gzip_comp_level 9; #压缩级别,压缩比率越高,文件被压缩的体积越小
gzip_types text/css application/javascript;#进行压缩的文件类型
root /data/www/html;
}
location ~ ^/download {
gzip_static on; #启用压缩
tcp_nopush on; # 不要着急发,攒一波再发
root /data/www; # 注意此处目录是`/data/www`而不是`/data/www/download`
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: 实现大整数相加
下一篇: 不要相信一个熬夜的人说的每一句话
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论