nginx.conf详解

来源:互联网 发布:网络刷手兼职可信吗 编辑:程序博客网 时间:2024/06/16 20:00

下面是nginx一些常用配置的说明,

nginx教程可以参考:

1、http://cnt1992.xyz/2016/03/18/simple-intro-to-nginx/

2、https://lufficc.com/blog/configure-nginx-as-a-web-server

3、http://seanlook.com/2015/05/17/nginx-install-and-config

4、http://gong1208.iteye.com/blog/1559835


# user字段表明了Nginx服务是由哪个用户哪个群组来负责维护进程的,默认是nobody# 查看当前用户命令: whoami# 查看当前用户所属组命令: groups ,当前用户可能有多个所属组,选第一个即可user nobody;# worker_processes字段表示Nginx服务占用的内核数量# 为了充分利用服务器性能你可以直接写你本机最高内核# 查看本机最高内核数量命令: sysctl -n hw.ncpuworker_processes  2;# error_log字段表示Nginx错误日志记录的位置# 项目多建议在server里单独配置# 模式选择:debug/info/notice/warn/error/crit# 上面模式从左到右记录的信息从最详细到最少#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#error_log  /usr/local/var/logs/nginx/error.log debug;#access_log  /usr/local/var/logs/nginx/access.log  main;# Nginx执行的进程id,默认配置文件是注释了# 如果上面worker_processes的数量大于1那Nginx就会启动多个进程# 而发信号的时候需要知道要向哪个进程发信息,不同进程有不同的pid,所以写进文件发信号比较简单# 你只需要手动创建,比如下面的位置: touch /usr/local/var/run/nginx.pidpid        /usr/local/var/run/nginx.pid;#工作模式及连接数上限events {    # 每一个worker进程能并发处理的最大连接数    # 当作为反向代理服务器,计算公式为: `worker_processes * worker_connections / 4`    # 当作为HTTP服务器时,公式是除以2    worker_connections  1024;}http {    # 设定mime类型,类型由mime.type文件定义    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可以让读取磁盘IO操作更快    # access_log  logs/access.log  main;    access_log off;    sendfile     on;    # 在一个数据包里发送所有头文件,而不是一个接一个的发送    tcp_nopush     on;    # 不要缓存    tcp_nodelay on;    # 连接超时时间    keepalive_timeout  65;    # 开启gzip压缩    # 一般不同项目单独配置, 写在server{}里单独配置    # gzip  on;    # gzip_disable "MSIE [1-6].";    # 设定请求缓冲    # 一般不同项目单独配置, 写在server{}里单独配置    # client_header_buffer_size    128k;    # large_client_header_buffers  4 128k;    # 将配置文件分离    # 配置不同站点的时候只需要在/usr/local/etc/nginx/sites-enabled/不断增加新的文件即可    include /usr/local/etc/nginx/sites-enabled/*;}#----------------------------------------#下面是/usr/local/etc/nginx/sites-enabled/下的一个default.confserver {    # Nginx监听端口号    listen 80;    # 服务器的名字,默认为localhost, 也可以写成XXXX.com    server_name  localhost;    # 前端代码放置的根目录    # VUE一般是直接把npm run build 构建好的dist文件放到/var/www/<project-name>/dist下    # 我这里放到/var/www/fe/下, (fe: front end)    root /var/www/fe;    # 首页索引文件名称: index    index index.html index.htm;    # 编码    charset utf-8;    # gzip    gzip            on;    # http://blog.csdn.net/jessonlv/article/details/8016284    # 设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。    # 默认值是0,不管页面多大都压缩。    # 建议设置成大于1k的字节数,小于1k可能会越压越大。    gzip_min_length 1000;    # gzip_proxied    # 语法: gzip_proxied [off|expired|no-cache|no-store|private|no_last_modified|no_etag|auth|any] ...    # 默认值: gzip_proxied off    # 作用域: http, server, location    # Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。    # off - 关闭所有的代理结果数据的压缩    # expired - 启用压缩,如果header头中包含 "Expires" 头信息    # no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息    # no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息    # private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息    # no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息    # no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息    # auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息    # any - 无条件启用压缩    gzip_proxied    expired no-cache no-store private auth;    # 匹配MIME类型进行压缩    gzip_types      text/plain application/xml application/json;    location / {        # try_files 最核心的功能是可以替代rewrite        # 按顺序检查文件是否存在,返回第一个找到的文件        # 如果所有的文件都找不到,会进行一个内部重定向到最后一个参数。        try_files $uri $uri/ @rewrites;        # 目录浏览功能  默认: off        # autoindex on;    }    location @rewrites {        # 语法:rewrite regex replacement [flag];        # 默认值:无        # 作用域:server,location,if        # 如果一个URI匹配指定的正则表达式regex,URI就按照replacement重写。        # rewrite按配置文件中出现的顺序执行。flags标志可以停止继续处理。        # last 停止处理后续rewrite指令集,然后对当前重写的新URI在rewrite指令集上重新查找。        # break 停止处理后续rewrite指令集,并不在重新查找,但是当前location内剩余非rewrite语句和location外的的非rewrite语句可以执行。        # redirect 如果replacement不是以http:// 或https://开始,返回302临时重定向        # permant 返回301永久重定向        rewrite ^(.+)$ /index.html last;    }    # 这个是设置缓存策略,把所有的静态文件,设置成在本地永久缓存    # location~.*\.(gif | jpg | js | css | jpeg | png | bmp | swf) $ {       # root / xxx/ xxx / xxx / dist;  //这块改成自己dist在的路径       # add_header Cache - Control max - age = 259200000000;    }    # 设置接口代理,‘/api’开头的请求接口反向代理到其他服务器    # location / api {       # rewrite ^ . + api / ? (.*) $ / $1       # break;       # include uwsgi_params;       # proxy_pass http: //japi.juhe.cn;    }    # 设置接口代理,‘/op’开头的请求接口反向代理到其他服务器    # location / op {       # rewrite ^ . + op / ? (.*) $ / $1       # break;       # include uwsgi_params;       # proxy_pass http: //op.juhe.cn;    # }    # 设置接口代理,‘/happy’开头的请求接口反向代理到其他服务器    # location / happy {       # rewrite ^ . + happy / ? (.*) $ / $1       # break;       # include uwsgi_params;       # proxy_pass http: //v.juhe.cn;    # }    #对 / 所有做负载均衡+反向代理    # location / {       # root   /apps/oaapp;       # index  index.jsp index.html index.htm;       # proxy_pass        http://backend;       # proxy_redirect off;       # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP       # proxy_set_header  Host  $host;       # proxy_set_header  X-Real-IP  $remote_addr;       # proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;       # proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;    # }    # 404页面跳转到404.html,相对于上面的root目录    error_page  404              /404.html;    # 403页面跳转到403.html,相对于上面的root目录    error_page  403              /403.html;    # 50x页面跳转到50x.html    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   html;    }}


原创粉丝点击