Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

1.7. FAQ

1.7.1. 405 Not Allowed?

1.7.1.1. 405 Not Allowed?

1.7.1.1.

405 Not Allowed?

靜態頁面POST會提示405 Not Allowed錯誤.

					
# curl -d name=neo http://www.mydoamin.com/index.html
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx</center>
</body>
</html>
					
					
server {
    listen       80 default;
    server_name  myid.mydomain.com;

    charset utf-8;
    access_log  /var/log/nginx/myid.mydomain.com.access.log  main;

    if ($http_user_agent ~* ^$){
      return 412;
    }
    ###########################

    location / {
        root   /www/mydomain.com/myid.mydomain.com;
        index  index.html index.php;
        #error_page 405 =200 $request_filename;
    }

    #error_page  404              /404.html;
    #
    error_page 405 =200 @405;
    location @405 {
        #proxy_set_header  Host            $host;
        proxy_method GET;
        proxy_pass http://myid.mydomain.com;

    }


    # 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;
    }
}
					

1.7.2. 502 Bad Gateway?

1.7.2.1. 502 Bad Gateway

1.7.2.1.

502 Bad Gateway

error.log 提示:

upstream sent too big header while reading response header from upstream?

修改fastcgi配置

location ~ \.php$ {

	fastcgi_buffers 8 16k;
	fastcgi_buffer_size 32k;
	。。。
	。。。
}
					

1.7.3. 413 Request Entity Too Large

1.7.3.1. 413 Request Entity Too Large

1.7.3.1.

413 Request Entity Too Large

error.log 提示:

client intended to send too large body

client_max_body_size 8m;

修改 /etc/nginx/nginx.conf 檔案。

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;

    server_tokens off;
    gzip  on;
    gzip_min_length 1k;
    gzip_types text/plain text/html text/css application/javascript text/javascript application/x-javascript text/xml application/xml application/xml+rss application/json;
    gzip_vary on;

    client_max_body_size 8m;

    include /etc/nginx/conf.d/*.conf;
}
					

1.7.4. 502 Bad Gateway?

1.7.4.1. 502 Bad Gateway

1.7.4.1.

502 Bad Gateway

error.log 提示:

upstream sent too big header while reading response header from upstream?

修改fastcgi配置

location ~ \.php$ {

	fastcgi_buffers 8 16k;
	fastcgi_buffer_size 32k;
	。。。
	。。。
}
					

1.7.5. 499 Client Closed Request

1.7.5.1. Nginx access.log 日誌顯示

1.7.5.1.

Nginx access.log 日誌顯示

111.85.11.15 - - [25/Jun/2016:19:20:35 +0800] "GET /xxx/xxx/xxx.jsp HTTP/1.1" 499 88 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36 JianKongBao Monitor 1.1"

配置 proxy_ignore_client_abort on;

    location / {

		ssi on;
		proxy_set_header Accept-Encoding "";
		proxy_pass http://127.0.0.1:8080;
        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_ignore_client_abort  on;
    }
					

1.7.6. proxy_pass

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/conf.d/www.mydomain.com.conf:25
nginx: configuration file /etc/nginx/nginx.conf test failed
		

在location,if中使用證則匹配proxy_pass末尾不能寫/

	if ($request_uri ~* "^/info/{cn|tw}/{news|info}/\d\.html") {
		proxy_pass http://info.example.com/;
		break;
	}

    location ~ ^/info/ {
                proxy_pass http://info.example.com/;
                break;
    }
		

proxy_pass http://info.example.com/; 改為 proxy_pass http://info.example.com; 可以解決

1.7.7. proxy_pass SESSION 丟失問題

如果用戶Cookie信息沒有經過 proxy_pass 傳遞給最終伺服器,SESSION信息將丟失,解決方案

proxy_set_header   Cookie $http_cookie;
		

1.7.8. [alert] 55785#0: *11449 socket() failed (24: Too many open files) while connecting to upstream

配置 worker_rlimit_nofile 參數即可

user  nginx;
worker_processes  8;
worker_rlimit_nofile 65530;
		

配置 ulimit 也能達到同樣效果,但我更喜歡 worker_rlimit_nofile 因為它僅僅作用於nginx,而不是全局配置。

1.7.9. server_name 與 SSI 注意事項

server_name www.example.com www.example.net www.example.org;
		

下來SSI標籤無論你使用那個域名訪問,輸出永遠是server_name的第一域名www.example.com

		
<!--#echo var="SERVER_NAME"-->
		
		

需要通過SERVER_NAME判定展示不同結果時需要注意。

1.7.10. location 跨 document_root 引用,引用 document_root 之外的資源

下面的例子是 Document root 是 /www/netkiller.com/m.netkiller.com, 我們需要 /www/netkiller.com/www.netkiller.com 中的資源。

		
server {
    listen       80;
    server_name  m.netkiller.com;

    charset utf-8;
    access_log  /var/log/nginx/m.netkiller.com.access.log;
    error_log  /var/log/nginx/m.netkiller.com.error.log;

    location / {
		root /www/netkiller.com/m.netkiller.com;
		index.html
    }

    location /module {
        root /www/netkiller.com/www.netkiller.com;
    }	

}

		
		
		
server {
    listen       80;
    server_name  m.netkiller.com;

    charset utf-8;
    access_log  /var/log/nginx/m.netkiller.com.access.log;
    error_log  /var/log/nginx/m.netkiller.com.error.log;

    location / {
		root /www/netkiller.com/m.netkiller.com;
		index.html
    }

    location ^~ /module/ {
        root /www/netkiller.com/www.netkiller.com;
    }	

}
		
		

上面的例子location /module 是指 /www/netkiller.com/www.netkiller.com + /module,如果 /www/netkiller.com/www.netkiller.com 目錄下面沒有 module 目錄是出現404, error.log顯示 "/www/netkiller.cn/www.netkiller.cn/module/index.html" failed (2: No such file or directory)

1.7.11. nginx: [warn] duplicate MIME type "text/html" in /etc/nginx/nginx.conf

text/html 是 gzip_types 預設值,所以不要將text/html加入到gzip_types列表內