nginxnewrelic

nginx - how to create /status with stub_status


I'm trying to create an /status to use with newrelic, but it's always returning 404.

$ curl 127.0.0.1/status
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.1</center>
</body>
</html>

Here is my nginx conf file (it's uses certbot as well).

server {

  server_name mysite.com api.mysite.com painel.mysite.com;

  location / {
    root   /var/www/mysite-prod/public;
    index  index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ \.(php|phar)(/.*)?$ {
    root /var/www/mysite-prod/public;
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
    fastcgi_intercept_errors on;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   php-fpm;
  }

  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;   # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf;                  # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;                    # managed by Certbot

}

server {

  location = /status {          <============== HERE
    stub_status  on;
    default_type text/plain;
    access_log   off;
    allow        127.0.0.1;
    deny all;
  }

  if ($host = panel.mysite.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot

  if ($host = api.mysite.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot

  if ($host = mysite.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot

  server_name mysite.com api.mysite.com painel.mysite.com;
  listen 80;

  return 404; # managed by Certbot

}

Am I doing something wrong?

I'm using AWS Linux and followed this guide: https://www.scalescale.com/tips/nginx/nginx-new-relic-plugin/#


Solution

  • Remove:

      location = /status {          <============== HERE
        stub_status  on;
        default_type text/plain;
        access_log   off;
        allow        127.0.0.1;
        deny all;
      }
    

    And just create a new config file status.conf with the following content:

    server {
        listen localhost;
        server_name status.localhost;
        keepalive_timeout 0;
    
        access_log off;
    
        allow 127.0.0.1;
        deny all;
    
        location /nginx_status {
            stub_status on;
        }
    }
    

    Reload Nginx config:

    sudo nginx -s reload
    

    Test the URL:

    $ curl http://127.0.0.1/nginx_status
    Active connections: 6
    server accepts handled requests
     1285282 1285282 17041299
    Reading: 0 Writing: 6 Waiting: 0
    

    New Relic config:

    url=http://localhost/nginx_status