Skip to main content

how to change nginx log_format main

Comments

12 comments

  • William Del Piero cPanel Staff

    Hi,

    To apply persistent customizations to NGINX, you'll need to create a .conf file within the following directory:

    /etc/nginx/conf.d/server-includes/
    

    You can find more details in our documentation here:

     

    https://docs.cpanel.net/knowledge-base/nginx/customize-reverse-proxy-nginx-configurations/#global-configuration-files

     

     

    That said, it looks like there may be syntax issues in the customizations you provided — or perhaps the full code wasn’t copied over. While we're happy to offer general guidance on implementing NGINX customizations, our ability to assist with writing or debugging specific configuration code is limited.

    0
  • Pietro Leone

    in the server-includes :

    nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/conf.d/server-includes/custom-format.main.conf:4

    outside in conf.d is working but i have duplicates entry on access.log.

    1 without host and 1 with host !!!

     

    0
  • cPRex Jurassic Moderator

    Hey there!  Can you let me know if you're using exactly the block of code above, and can you also tell me the specific include file you're using so I can test this?

    0
  • Pietro Leone

    here is it:

        log_format  custom_main  '$remote_addr - $remote_user [$time_local] "$request" '
                                 '$status $body_bytes_sent "$http_referer" '
                                 '"$http_user_agent" "$http_x_forwarded_for" '
                                 'host=$host';

        access_log  /var/log/nginx/access.log  custom_main;

     

    and this 1 couple of it in the access.log after :

    101.xxx.yyy.zzz - - [03/Nov/2025:16:34:16 +0100]   "GET /wp-content/plugins/armember/fonts/fa-regular-400.woff2 HTTP/2.0" 200 13548 "https://myhost.it/wp-content/plugins/armember/css/arm-font-awesome.css?ver=6.8.2"
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36" "-"


    101.xxx.yyy.zzz - - [03/Nov/2025:16:34:16 +0100] "GET /wp-content/plugins/armember/fonts/fa-regular-400.woff2 HTTP/2.0" 200 13548 "https://myhost.it/wp-content/plugins/armember/css/arm-font-awesome.css?ver=6.8.2"
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36" "-" host=myhost.it

     

     

     

    0
  • cPRex Jurassic Moderator

    Let me do some testing with this on my end and I'll get back to you in a bit!

    0
  • cPRex Jurassic Moderator

    I created a version 132 machine with nginx installed, and then did the following:

    -created /etc/nginx/conf.d/custom_file.conf
    -added your code to the file:

        log_format  custom_main  '$remote_addr - $remote_user [$time_local] "$request" '
                                 '$status $body_bytes_sent "$http_referer" '
                                 '"$http_user_agent" "$http_x_forwarded_for" '
                                 'host=$host';

        access_log  /var/log/nginx/access.log  custom_main;

    -restarted nginx:

    [root@a9132 conf.d]# /scripts/restartsrv_nginx 
    Waiting for “nginx” to restart gracefully …waiting for “nginx” to initialize ………finished.

    Service Status
        nginx (nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf) is running as root with PID 2351101 (systemd+/proc check method).

    Startup Log
        Nov 03 15:54:03 a9132.openstack.build systemd[1]: Starting nginx - high performance web server...
        Nov 03 15:54:03 a9132.openstack.build systemd[1]: nginx.service: Can't open PID file /run/nginx.pid (yet?) after start: Operation not permitted
        Nov 03 15:54:03 a9132.openstack.build systemd[1]: Started nginx - high performance web server.
        Nov 03 15:54:08 a9132.openstack.build systemd[1]: Reloading nginx - high performance web server...
        Nov 03 15:54:08 a9132.openstack.build systemd[1]: Reloaded nginx - high performance web server.
        Nov 03 15:57:28 a9132.openstack.build systemd[1]: Reloading nginx - high performance web server...
        Nov 03 15:57:28 a9132.openstack.build systemd[1]: Reloaded nginx - high performance web server.

    nginx restarted successfully.

    and I'm not seeing any syntax issues on my end.  Are you doing something different on your side?

    0
  • Pietro Leone

    i do not have syntax error i have duplicates entries on access.log

     

    0
  • cPRex Jurassic Moderator

    Isn't that what your code is doing?  You're not overwriting the default log, you're adding an additional logging section, so I would expect there to be duplicates with this configuration.

    Can you let me know specifically what you're trying to change from the default?  Maybe I can find an alternative workaround for that change.

    0
  • Pietro Leone

    grazie mille cPRex,

    My goal is to add the value of $host to each line of access.log. I don't want to add a new log line, but I want to modify the original one........

    0
  • cPRex Jurassic Moderator

    Inside /etc/nginx/conf.d/server-includes/ have you tried creating a file named custom.conf and adding just this line?

    log_format  custom_main  'host=$host';

    If that doesn't do it, we may need to see a ticket to check things on your environment directly.

    0
  • Pietro Leone

    # nginx -t
    nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/conf.d/server-includes/log_format.conf:1
    nginx: configuration file /etc/nginx/nginx.conf test failed

    i will update with ticket

    0
  • cPRex Jurassic Moderator

    I'm honestly not sure at this point - could you create a ticket for this issue?

    0

Please sign in to leave a comment.