• Nginx报错汇总


    1.     Nginx 无法启动解决方法

    在查看到 logs 中报了如下错误时:

    0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions) 

    是因为 80 端口出现了冲突

    2.     Weight  和 ip_hash

    在负载均衡均衡模块中 upstream 的

    Weight 是可以把请求的链接优先访问该服务

    Ip_hash 会去将客户端转发到一个可用的服务器上

    如果服务器不可用需要用 down 来标注

    还有就是 weight 和 ip_hash 不能同时使用

    3.     Nginx tomcat 负载均衡和多域名同端口转发

    Nginx 进行 http 负载均衡的模块是 upstream

    Upstream 可以进行多个配置,这样的话可以灵活的配置站点,但是注意的是 upstream后面的名字最好是配置成为域名,因为 upstream 是进行 http 访问的,一般的解析没有问题,但是如果是ajax 的解析就会通过访问 upstream 后面的名字来进行访问了,这里要注意。 

    修改配置文件 :conf/nginx.conf 

    Upstream 的 server 的配置:

    §    weight = NUMBER -  设置服务器权重,默认为 1 。

    §    max_fails = NUMBER -  在一定时间内(这个时间在 fail_timeout 参数中设置)检查这个服务器是否可用时产生的最多失败请求数,默认为 1 ,将其设置为 0 可以关闭检查,这些错误在 proxy_next_upstream 或 fastcgi_next_upstream ( 404 错误不会使max_fails 增加)中定义。

    §    fail_timeout = TIME -  在这个时间内产生了 max_fails 所设置大小的失败尝试连接请求后这个服务器可能不可用,同样它指定了服务器不可用的时间(在下一次尝试连接请求发起之前),默认为 10 秒, fail_timeout与前端响应时间没有直接关系,不过可以使用 proxy_connect_timeout 和 proxy_read_timeout 来控制。

    §    down -  标记服务器处于离线状态,通常和ip_hash 一起使用。

    §    backup - (0.6.7 或更高 ) 如果所有的非备份服务器都宕机或繁忙,则使用本服务器(无法和ip_hash 指令搭配使用)。

    # 需要进行负载均衡的站点

    # 其中 server 是其中负载均衡的一个节点 www.aaa.com

    upstream www.aaa.com { 

            server 192.168.0.1:8080 weight=1; 

    server 192.168.0.2:8080 weight=2;

    server 192.168.0.1:8081 weight=3; 

    # 第二个网站的 www.bbb.com的负载均衡的节点

    upstream www.bbb.com { 

            server 192.168.1.1:8080 ; 

    server 192.168.1.2:8080 ; 

    server 192.168.1.3:8080 ; 

            ip_hash; 

     } 

    # 同一服务器转发 2 个不同域名进行负载均衡

    #www.aaa.com 的 server

    server

      {

        listen       80;

        server_name  www.aaa.com;

      

                  location / { 

                index  index.html index.jsp; 

                         # 这里的 proxy_pass 转发的是 upstream 的名字 www.aaa.com

                proxy_pass  http://www.aaa.com; 

                proxy_set_header    X-Real-IP   $remote_addr; 

                client_max_body_size    100m; 

            }    

        #limit_conn   crawler  20;    

    server

      {

        listen       80;

        server_name  www.bbb.com; 

                  location / { 

                index  index.html index.jsp; 

    # 这里的 proxy_pass 转发的是 upstream 的名字 www.bbb.com

                proxy_pass  http://www.bbb.com; 

                proxy_set_header    X-Real-IP   $remote_addr; 

                client_max_body_size    100m; 

            }  

        #limit_conn   crawler  20;    

    4.     nginx 安装 linux

    先决条件

    yum install pcre pcre-devel

    释放文件

    tar –zxvf nginx.tar.gz 

    安装

    ./ configure –prefix= 路径

    make

    make install

    3 )管理 nginx 服务

    启动:

    /usr/local/nginx/sbin/nginx

    停止

    /usr/local/nginx/sbin/nginx -s stop

    重启

    /usr/local/nginx/sbin/nginx  -s reload

    查看状态

    ps -auxf | grep nginx 

    ./configure     "--prefix=/export/servers/nginx"     "--sbin-path=/export/servers/nginx/sbin/nginx"     "--conf-path=/export/servers/nginx/conf/nginx.conf"     "--error-log-path=/export/servers/nginx/logs/error.log"     "--http-log-path=/export/servers/nginx/logs/access.log"     "--pid-path=/export/servers/nginx/var/nginx.pid"     "--lock-path=/export/servers/nginx/var/nginx.lock"     "--http-client-body-temp-path=/dev/shm//nginx_temp/client_body"     "--http-proxy-temp-path=/dev/shm/nginx_temp/proxy"     "--http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi"     "--user=www"     "--group=www"     "--with-cpu-opt=pentium4F"     "--without-select_module"     "--without-poll_module"     "--with-http_realip_module"     "--with-http_sub_module"     "--with-http_gzip_static_module"     "--with-http_stub_status_module"     "--without-http_ssi_module"     "--without-http_userid_module"     "--without-http_geo_module"    "--without-http_map_module"     "--without-mail_pop3_module"     "--without-mail_imap_module"     "--without-mail_smtp_module"

    5.     日志输出相应时间

    "$request_time"';

    在 log_format  后面添加上上面的参数即可

    log_format main            '$remote_addr - $remote_user [$time_local] '

                                                           '"$request" $status $bytes_sent '

                                                           '"$http_referer" "$http_user_agent" '

                                                              '"$gzip_ratio"'; 

    6.     设置 cache-control

    Http协议的 cache-control 的常见取值及其组合释义 :

    no-cache:  数据内容不能被缓存 ,  每次请求都重新访问服务器 ,  若有 max-age,  则缓存期间不访问服务器 .

    no-store:  不仅不能缓存 ,  连暂存也不可以 ( 即 :  临时文件夹中不能暂存该资源 )

    private( 默认 ):  只能在浏览器中缓存 ,  只有在第一次请求的时候才访问服务器 ,  若有 max-age,  则缓存期间不访问服务器 .

    public:  可以被任何缓存区缓存 ,  如 :  浏览器、服务器、代理服务器等

    max-age:  相对过期时间 ,  即以秒为单位的缓存时间 .

    no-cache, private:  打开新窗口时候重新访问服务器 ,  若设置 max-age,  则缓存期间不访问服务器 .

    private,  正数的 max-age:  后退时候不会访问服务器

    no-cache,  正数的 max-age:  后退时会访问服务器

    点击刷新 :  无论如何都会访问服务器 .

    Expires:

    设置以分钟为单位的绝对过期时间 ,  优先级比 Cache-Control 低 ,  同时设置 Expires 和 Cache-Control 则后者生效 .

    Last-Modified:

    该资源的最后修改时间 ,  在浏览器下一次请求资源时 ,  浏览器将先发送一个请求到服务器上 ,  并附上 If-Unmodified-Since 头来说明浏览器所缓存资源的最后修改时间,  如果服务器发现没有修改 ,  则直接返回 304(Not Modified)回应信息给浏览器 ( 内容很少 ),  如果服务器对比时间发现修改了 ,  则照常返回所请求的资源 .

    在网页中设置:

    <meta http-equiv="Cache-Control" content="max-age=7200" />

    <meta http-equiv="Expires" content="Mon, 20 Jul 2009 23:00:00 GMT" /> 

    只对本网页有效 

    Nginx  设置:

    #  相关页面设置 Cache-Control 头信息 

    if ($request_uri ~* "^/$|^/search/.+/|^/company/.+/") {

      add_header    Cache-Control  max-age=3600;

    if ($request_uri ~* "^/search-suggest/|^/categories/") {

      add_header    Cache-Control  max-age=86400;

    全局的就在 location / 下面配置即可

    7.     静态压缩和动态压缩的区别

    静态压缩 :

    静态压缩是之间就通过工具将文件通过压缩工具进行压缩,然后nginx 只是做文件头设置即可;如:

    文件为 1.html ,然后压缩后为 1.html.gz

    然后 nginx 的配置文件中配置:

    location ~ .gz$ {

    add_header  Content-Encoding  gzip;gzip off;//这里的 off 是不进行动态压缩

    }

    因为之前我们的 1.html 已经压缩为 1.html.gz 了,所以这时我们只需要设置header 为 gzip 即可,不开启 gzip 动态压缩; 

    动态压缩:

    动态压缩就是我们的文件之前不通过工具压缩,而通过 nginx 进行压缩,这样的为动态压缩,如: 

    #启动预压缩功能,对所有类型的文件都有效   gzip_static on;   
      
    #找不到预压缩文件,进行动态压缩   gzip on;    
    gzip_min_length 1000;   
    gzip_buffers 4 16k;   
    gzip_comp_level 5;   
    gzip_types text/plain application/x-javascript text/css application/xml;   
      
    #gzip公共配置     gzip_http_version 1.1  
    gzip_proxied expired no-cache no-store private auth;   

    #纠结的配置     
    # 对于 ie 有个 bug ,响应vary头后将不会缓存请求,每次都会重新发新的请求。所以,对于ie 1-6直接禁用 gzip 。    
    gzip_disable "MSIE [1-6].";    
    # 开启 Http Vary 头,vary头主要提供给代理服务器使用,根据Vary头做不同的处理。例如,对于支持gzip的请求反向代理缓存服务器将返回gzip内容,不支持 gzip 的客户端返回原始内容。    
    gzip_vary on;  

    1.      gzip_static配置优先级高于 gzip

    2.      开启nginx_static后,对于任何文件都会先查找是否有对应的gz文件

    3.      gzip_types设置对 gzip_static 无效

    8.     nginx: [emerg] unknown directive "if"

    nginx: [emerg] unknown directive "if($args" in /export/servers/nginx/conf/nginx.conf:90

    这个错误是因为缺少  pcre 的包

    9.     安装 nginx pcre 问题解决方法

    pcre 需要指定路径

     -with-pcre=/usr/local/include/pcre

    注意后面不要加斜杠 

    首先,

    mkdir  /usr/local/include/pcre

    mkdir /usr/local/include/pcre/.libs

    然后把 pcre 的包 cp 到指定路径下:

    cp /opt/pcre/lib/libpcre.a /usr/local/include/pcre/libpcre.a

    cp /opt/pcre/lib/libpcre.a /usr/local/include/pcre/libpcre.la

    cp /opt/pcre/include/pcre.h /usr/local/include/pcre/pcre.h

    cp  /usr/local/include/pcre/*.* /usr/local/include/pcre/.libs 

    /opt/pcre 是 pcre 的安装路径这里可自定, cp 完文件后,我们将 nginx 配置安装

    ./configure --prefix=/opt/nginx --with-pcre=/usr/local/include/pcre 

    注意:

    --with-pcre=/usr/local/include/pcre

    这里必须是 /usr/local/include/pcre 路径,安装路径不行,这里尝试过了,否则失败后很头疼; 

    第二当编译好后,报如下错误:

    make -f objs/Makefile

    make[1]: Entering directory `/export/software/nginx/jdws-1.0'

    cd /usr/local/include/pcre

            && if [ -f Makefile ]; then make distclean; fi

            && CC="gcc" CFLAGS=""

            ./configure --disable-shared

    /bin/sh: line 2: ./configure: ûÓÐÄǸöÎļþ»òĿ¼

    make[1]: *** [/usr/local/include/pcre/Makefile] ´íÎó 127

    make[1]: Leaving directory `/export/software/nginx/jdws-1.0'

    make: *** [build] ´íÎó 2 

    我们修改 nginx 安装目录下的 objs/MakeFile 文件中的

    /usr/local/include/pcre/Makefile:   objs/Makefile

         cd /usr/local/include/pcre

          && if [ -f Makefile ]; then $(MAKE) distclean; fi

          && CC="$(CC)" CFLAGS=""

    ./configure --disable-shared

    大约在 994 行,删除 ./configure --disable-shared 这行内容后,然后 make  和 make install 就没有问题了

    10.           Nginx 反向代理获取真实 IP

    proxy_set_header Host $host;

    proxy_set_header X-Forwarded-For $remote_addr;

    11.           nginx  使用 if 错误

    当出现如下错误是:

    [emerg]: unknown directive "if($request_method" in /export/servers/nginx/conf/nginx.conf:86

    一种是少 pcre 包

    另一种是:

    if  和括号间需要空格

    12.             设定 cache 头

    add_header X-Cache '$upstream_cache_status from $server_addr'; 

    13.           nginx cache 状态缓存

    proxy_cache_valid  200 304 20m;

    如果要缓存内容,需要加上如上内容,这是对 http 状态值进行缓存,并且设定缓存时间。 

    14.           $request_uri

    $request­_uri 获取当前浏览器完整路径 

    $uri$is_args$args  这个是获取真实请求路径

    15.           Rewrite 需要写在 location

    用 rewrite 报 404

    后来查看到配置中 rewrite  在 server 内 location 外,这样是有问题的,把 rewrite 放到 location 中就没问题

    16.           调试 nginx

    修改 config 并编译

    由于 gdb 需要 gcc 的时候加上 -g 参数,这样生成的文件才能使用 gdb 调试,因此我们要对源码做一下小改动

    修改 auto/cc/conf 文件

    ngx_compile_opt="-c"

    变为

    ngx_compile_opt="-c -g"

    执行 configure

    ./configure --prefix=/home/yejianfeng/nginx/

    确认

    发现多出了 objs 文件夹,里面有 Makefile 文件

    确认一下 -g 参数是否加上了

    vim objs/Makefile

    clip_image001[6]

    17.           Nginx  高并发设置

    并发 100 时, nginx+php  可以到 850 ,但是 300-500 并发却低到 400-500 ,这不正常,所以需要对 linux 系统的环境进行设置

    ================================================================

    vi /etc/sysctl.conf  CentOS5.5 中可以将所有内容清空直接替换为如下内容 :

    net.ipv4.ip_forward = 0

    net.ipv4.conf.default.rp_filter = 1

    net.ipv4.conf.default.accept_source_route = 0

    kernel.sysrq = 0

    kernel.core_uses_pid = 1

    net.ipv4.tcp_syncookies = 1

    kernel.msgmnb = 65536

    kernel.msgmax = 65536

    kernel.shmmax = 68719476736

    kernel.shmall = 4294967296

    net.ipv4.tcp_max_tw_buckets = 6000

    net.ipv4.tcp_sack = 1

    net.ipv4.tcp_window_scaling = 1

    net.ipv4.tcp_rmem = 4096 87380 4194304

    net.ipv4.tcp_wmem = 4096 16384 4194304

    net.core.wmem_default = 8388608

    net.core.rmem_default = 8388608

    net.core.rmem_max = 16777216

    net.core.wmem_max = 16777216

    net.core.netdev_max_backlog = 262144

    net.core.somaxconn = 262144

    net.ipv4.tcp_max_orphans = 3276800

    net.ipv4.tcp_max_syn_backlog = 262144

    net.ipv4.tcp_timestamps = 0

    net.ipv4.tcp_synack_retries = 1

    net.ipv4.tcp_syn_retries = 1

    net.ipv4.tcp_tw_recycle = 1

    net.ipv4.tcp_tw_reuse = 1

    net.ipv4.tcp_mem = 94500000 915000000 927000000

    net.ipv4.tcp_fin_timeout = 1

    net.ipv4.tcp_keepalive_time = 30

    net.ipv4.ip_local_port_range = 1024 65000

    使配置立即生效可使用如下命令:

    sysctl -p

    ==============================================================

    在 /etc/security/limits.conf 最后增加:

    * soft nofile 65535

    * hard nofile 65535

    * soft nproc 65535

    * hard nproc 65535

    具体使用哪种,在  CentOS  中使用第 1  种方式无效果,使用第 3  种方式有效果,而在 Debian  中使用第 2  种有效果 

    参考:

    http://hi.baidu.com/touchiyudeji/item/a699730b80ba78d9dde5b00e 

    18.           nginx  日志格式配置 

           log_format main            '$remote_addr - $remote_user [$time_local] ' '-----"$request"------- $status $bytes_sent '

                                                           '"$http_referer" "$http_user_agent" '

                                                              '"$gzip_ratio"' '"addr:$upstream_addr - status:$upstream_status - cachestatus:$upstream_cache_status"'

                                                                                                      '- cacheKey:"$host:$server_port$request_uri"' ;

    请求的 upstream  的地址 $upstream_addr

    请求 upstream  的状态 $upstream_status

    请求的 upstream  的 cache 的状态 $upstream_cache_status

    19.           Nginx if  判断条件中的分组()不能超过 9 个,超过 9个再非 rewrite 的会出现 aborted 问题

    if ($request ~* .*/((1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9))){

    }

    这种情况下,非 rewrite 的内容将显示 aborted 问题,解决方式由多个 if 进行判断

    20.           error_page  切换到指定的 location

    定义一个 location , 用 @ 定义一个名称是外面无法访问的,一般用在try_files 和 error_page 中

    location @hhvm_error_to_php {

                    include                 fastcgi_params;

       #             fastcgi_pass            unix:/dev/shm/php-fcgi.sock;

                    fastcgi_pass            php_servers;

                    fastcgi_index           index.php;

                    fastcgi_param           SCRIPT_FILENAME /export/data/www/comm.360buy.com_test$fastcgi_script_name;

                    fastcgi_connect_timeout 3;

                    fastcgi_send_timeout 5;

                    fastcgi_read_timeout 5;

            }

    location ~ .php$ {

               #################hhvm###############################################################################

                #edit date:20130724

                #edit author:huzhiguang

                #function: access url to hhvm

               ###################################################################################################

            if ($request ~* .*/(ProductPageService.aspx|clubservice.aspx|(productpage/p-(d*)-s-(d*)-t-(d*)-p-(d*).html.*)|(clubservice/newcomment-(.*)-(d*).html.*))){

    # 当遇到错误时 500 502 503 504 时跳转到 hhvm_error_to_php 这个 location 由 php 处理

                            error_page 500 502 503 504 = @

                            proxy_pass       http://hhvms ;

                  # 当 if 匹配中使用了 break , 则下面不会再继续进行匹配,那么也就不用去判断非了

                            break;

            }

                  # 加上改行后, proxy 返回 500 后,会拦截进行 error_page 处理,默认不处理

            proxy_intercept_errors on;

            proxy_set_header Host $host;

            proxy_set_header X-Forwarded-For $remote_addr;

               ##################################################################################################

                    include                 fastcgi_params;

     #               fastcgi_pass            unix:/dev/shm/php-fcgi.sock;

                    fastcgi_pass            php_servers;

                    fastcgi_index           index.php;

                    fastcgi_param           SCRIPT_FILENAME /export/data/www/comm.360buy.com_test$fastcgi_script_name;

                    fastcgi_connect_timeout 3;

                    fastcgi_send_timeout 5;

                    fastcgi_read_timeout 5;

             }

    如果加上该行, php 如果出现了 500 , nginx 会进行 error_page 处理,默认不处理

    fastcgi_intercept_errors on;

    21.           Nginx 遇到访问 aborted 解决

    当 nginx 访问所有的 aborted 都不行时,配置又没有问题,reload 也不生效时,然后将所有的 nginx 进行 kill 掉,然后重启就好了,

    这个是我发现因为我更新了一个动态链接库:

    libz.so.1 => /export/servers/hhvm-1.1/support_lib/libz.so.1 (0x00007fd912710000)

    然后影响了 nginx 的运行,所以造成了这种 reload 无效的问题

  • 相关阅读:
    POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)
    防止登录页面出如今frame中
    android--显式跳转和隐式跳转的差别使用方法
    卫星照片
    poj 2586 Y2K Accounting Bug (贪心)
    【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
    【转】在Eclipse中使用PyDev进行Python开发
    【转】eclipse + Pydev 配置Python开发环境
    【转】Python自动化测试 (一) Eclipse+Pydev 搭建开发环境
    【转】Eclipse的启动问题【an error has occurred see the log file】
  • 原文地址:https://www.cnblogs.com/qingtian224/p/9001787.html
Copyright © 2020-2023  润新知