使用Nginx作为代理,ajax请求每隔一个请求得到405

时间:2016-08-26 04:33:34

标签: ajax tomcat nginx

我正在使用nginx来分隔静态和动态内容, 这是我的nginx配置:



    #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    error_log  /usr/local/var/logs/nginx/error.log  notice;
    #error_log  logs/error.log  info;

    pid        /usr/local/var/logs/nginx/nginx.pid;

    events {
        worker_connections  1024;
    }

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

        log_format  main  '$remote_addr:$remote_port - [$request_method] [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" '
                          'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

        access_log  /usr/local/var/logs/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        upstream tomcat_backend {
            server localhost:8090;
        }

        server {
            listen 9797;
            server_name localhost;
            server_name_in_redirect off;
            access_log /usr/local/var/logs/nginx/test.access.log main;
            error_log  /usr/local/var/logs/nginx/test-error.access.log  debug;
            location ~ \.(jpg|png|js|ico|html|gif|css|map)$ {
                root /path/to/static;
            }
            location ^~ /api/ {
                access_log /usr/local/var/logs/nginx/nginx-location.access.log main;
                error_log  /usr/local/var/logs/nginx/nginx-location-error.access.log  debug;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_set_header X-Request-Time $msec;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_pass http://tomcat_backend;
                #proxy_set_header X-Request-Id $request_id;
            }
            location / {
                root /path/to/static;
                index index.html;
                if (!-e $request_filename) {
                    rewrite ^/(.*)  /index.html last;
                }

            }
        }
        include servers/*;
    }

有时tomcat可以正确获取请求和响应,有时nginx只是直接返回405,而不是事件转发到tomcat

这是chrome的控制台,它总是遵循那个模式,405,401,405,401 ......,它显示401是因为我输入了错误的密码,这意味着请求已转发到tomcat: enter image description here

我发现如果我使用node.js作为后端(只是一个代理服务器),一切都好,不再是405了。

1 个答案:

答案 0 :(得分:1)

好的,最后我找到了原因。

还有另一个进程正在监视同一个端口。

但有两个进程正在使用相同的端口