Nginx frequently returns 404 PHP FPM

Posted July 24, 2017 13.5k views

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;    

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


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

@Mohsen47 @hansen
Below are some latest http requests: - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 200 55 "-" "okhttp/3.6.0" - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 200 56 "-" "okhttp/3.6.0" - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 200 55 "-" "okhttp/3.6.0" - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 404 151 "-" "okhttp/3.6.0" - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 404 151 "-" "okhttp/3.6.0" - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=addAction HTTP/1.1" 200 54 "-" "okhttp/3.6.0" - - [24/Jul/2017:12:41:18 +0000] "POST /index.php?mode=showCoins HTTP/1.1" 404 151 "-" "okhttp/3.2.0" - - [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


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: - The wizard is a good, easy tool - 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.