Nginx frequently returns 404 PHP FPM

July 24, 2017 582 views
Nginx PHP Ubuntu

I have a server with Nginx 1.4 in Ubuntu 14.04 and it's running with PHP5-fpm. The server has multiple domains and sub-domains in a Private cloud server. The server is getting consistent hits from traffic and it's lot. Everything is fine, but Nginx frequently returns 404, while the case is not in actual and after couple of try it returns the actual output instead of 404. Below is one example of my one of the server block from nginx config.

server {
    listen 80;
    #listen [::]:80;

    root /var/www/appfolder;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name domain-name;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/appfolder;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #   # With php5-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
   }

Please help me on this, I have searched a lot, but never got a proper solution. Thanks in advance.

2 comments
  • @rajenpal033

    For the same urls it returns 404 and sometimes 200??

    Show us your access and error logs in these two situations

    tail -30 /var/log/nginx/access.log

    tail -30 /var/log/nginx/error.log

    So we can help you.

  • @rajenpal033 Like said before, we need to see your logs showing both 200 and 404.
    Also, can you run this command to show RAM details free -h

2 Answers

@Mohsen47 @hansen
Below are some latest http requests:

47.62.115.161 - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 200 55 "-" "okhttp/3.6.0"
112.215.65.176 - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 200 56 "-" "okhttp/3.6.0"
120.188.93.200 - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 200 55 "-" "okhttp/3.6.0"
112.215.240.62 - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 404 151 "-" "okhttp/3.6.0"
31.39.201.71 - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 404 151 "-" "okhttp/3.6.0"
212.96.90.104 - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 200 54 "-" "okhttp/3.6.0"
217.73.129.122 - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=showCoins HTTP/1.1" 404 151 "-" "okhttp/3.2.0"
77.4.213.189 - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 404 151 "-" "okhttp/3.6.0"

You can see for the same action it's getting 404 and 200 both but 404 maximum time.

  • @rajenpal033

    What do you mean by "404 maximum time" ?

    How many requests do you have per second? Do you see any Nginx error logs?
    Run this command free -h and paste the output here.

    • I mean 404 is coming frequently. Per second total requests are around 1000. For error log there are some php warnings

      here is the result of free -h :

                          total       used       free     shared    buffers     cached
      Mem:           15G        11G       4.4G        36M       174M        10G
      -/+ buffers/cache:       690M        14G
      Swap:         1.9G        37M       1.9G
      
      • @rajenpal033

        My first guess would be that PHP doesn't have enough processes to handle that amount of requests, but then you should see 502 or another 5xx error.
        What does your PHP-FPM pool look like?

        You would get a lot of speed improvements by upgrading to PHP7+ and Nginx 1.10+, specially when you have this amount of requests.

        Could it be that your /index.php?mode=addAction returns 404 be mistake because it hit another error somewhere (maybe database connection failed)?
        Has this always been a problem or is it something new? If it's a new problem, what changed?

        • Yes you are right, this is probably due to mysql connection, because CPU usage is fine 73% and the RAM is also used 25%. Now I have max_connections = 151 in MySQL with thread _ cache _ size = 15 and back _ log = 1000. What change should I do exactly to have a optimised MySQL environment for 1000 hits per seconds?

          Thanks in advance

@rajenpal033

If the problem is with the database connection, then it's very strange, that the script returns 404-errors. You need to verify that.

1) What does your PHP-FPM pool look like?
2) Has this always been a problem or is it something new? If it's a new problem, what changed?
3) Do you have any debug logging of the script code?

To scale MySQL for a 1000 connections doesn't require more than changing the max_connections = 1001, but there a lot of other parameters to look at and it depends on read-write levels:
https://tools.percona.com/ - The wizard is a good, easy tool
https://github.com/major/MySQLTuner-perl - The tuner gives you some good hints

  • This problem is going on with the number of traffic raises, I will check by increasing the max_connections a bit from my current settings and really thanks for your help.

Have another answer? Share your knowledge.