nginx 基础配置

242 阅读1分钟

本文将介绍基础配置项,location写法。

配置项

http {
    include       mime.types;
    default_type  application/octet-stream;

    server_names_hash_bucket_size 128;
    #server_tag off;
    #server_info off;
    server_tokens off;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    fastcgi_connect_timeout 5;
    fastcgi_send_timeout 10;
    fastcgi_read_timeout 10;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    keepalive_timeout  60;
    keepalive_requests 1024;
    client_header_buffer_size 4k;
    large_client_header_buffers 4 32k;
    client_max_body_size 60m; // 客户端上传文件的最大限制

    client_body_buffer_size 512k;
    client_body_timeout 45;
    client_header_timeout 10;
    send_timeout 240;

    proxy_connect_timeout   10s;
    proxy_send_timeout      120s;
    proxy_read_timeout      120s;
    proxy_buffers           64 8k;
    proxy_busy_buffers_size    128k;
    proxy_temp_file_write_size 64k;
    proxy_redirect off;
    # proxy_next_upstream_tries 1;
    proxy_next_upstream error invalid_header timeout http_502 http_504;

    gzip on; // 开启资源压缩提升传输速度 前端页面性能优化的一种手段
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css text/xml application/xml+css application/json text/javascript;
    gzip_vary on;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Real-Port $remote_port;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass_header Server;

    log_format main '$server_addr\t$host\t'
        '$remote_addr\t$http_x_forwarded_for\t'
        '$time_local\t'
        '$scheme\t$request\t'
        '$status\t$upstream_status\t'
        '$request_time\t$upstream_addr\t$upstream_response_time\t'
        '$request_length\t$bytes_sent\t'
        '$http_referer\t$http_cookie\t$http_user_agent\t'
        '$limit_rate\t$http_didi_header_omgid\t$remote_port';


    set_real_ip_from 10.0.0.0/8;
    set_real_ip_from 100.64.0.0/10;
    real_ip_header X-Real-IP;
    server {
        listen       8080 backlog=4096;
        underscores_in_headers on; // 设置on用于支持Headers带下划线
        server_name  localhost;
        access_log logs/access.log main;
        
        root /root/product;
        location / {
            root /root/product;
            if ($request_filename ~* .*\.(?:htm|html|json)$) {
                add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
            }
            try_files $uri /index.html;
        }

        location /product {
          alias  /root/product;
          try_files $uri @fallback;
        }
        location @fallback {
          root /root/product/;
          rewrite .* /index.html break;
        }

        location /test/index.html {
            proxy_pass http://xxx/index.html; // 代理地址
            proxy_hide_header Cache-Control;
            add_header Cache-Control  "no-cache";
        }

        location /test {
            rewrite ^.*$ /test/index.html; //重写到上面的location
        }

        location ~ ^/api/op/ {
            if ($http_x_data_center = "us") { //$http_x_data_center(一律采用小写)使用前提 underscores_in_headers on
                proxy_pass http://xxx:8080;
                break; //停止往下匹配
            }
            proxy_pass http://127.0.0.1:8008;
        }

        location ~ ^/op/ {
            rewrite ^/op/(.*) $1  break; // 去掉/cn/前缀,比如/op/api/ -> api/
            proxy_pass http://127.0.0.1:8008/$uri$is_args$args;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    }

location的匹配指令

类型说明优先级
=普通字符精确匹配9
^~普通字符匹配非正则,如果该选项匹配则不匹配别的选项,一般用来匹配目录8
~*正则匹配,不区分大小写7
~正则匹配,区分大小写7
/**常规字符串匹配类型,按前缀匹配。匹配完还会去看有没有正则表达式匹配和更长的匹配6
@定义一个命名的location,使用在内部定向时,例如 error_page, try_files

alias与root区别

官方root

类型配置请求地址实际请求地址
rootloaction /i/ { root /data/w3; }/i/top.gift/data/w3/i/top.gift
aliasloaction /i/ { alias/data/w3/; }/i/top.gift/data/w3/top.gift

nginx常见错误及解决方案

  • nginx: [error] invalid PID number "" in "/run/nginx.pid" 解决方法:启动前先执行nginx -c /etc/nginx/nginx.conf