Nginx multiple access_log / multiple same server_name

August 22, 2016 217 views
Nginx Logging Ubuntu


Actually i have one site and two servers in Nginx conf file.

The first:

 server {
        listen   80 default;
        server_name  localhost;
        access_log off;
        rewrite ^ https://$http_host$request_uri? permanent;    # force redirect http to https

The second

server {
       listen   443 ssl;
       server_name  localhost;

       ssl  on;
       ssl_certificate  /etc/nginx/certificates/test.crt;
       ssl_certificate_key   /etc/nginx/certificates/test.key;
       access_log /home/collecte.log;
       root   /var/www;

       location / {
                index  index.php index.html index.htm;

        location ~ \.php$ {

                if ( $request ~* myString){
                         access_log off;
                        # access_log /home/collecte2.log;

                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
                include fastcgi_params;

        location ~* \.(ico|css|js|gif|jpg|jpeg|png)$ {
                expires 1d;
                add_header Pragma public;
                add_header Cache-Control "public";

The idea is to separate requests in two files.
Like in the second server i disabled the log for same requests and i want to save them in another access_log file.

I tried access_log /home/collecte2.log; but it doesn’t work.
Does anyone have any idea if i can duplicate the server with same configuration or with a different server name? How can i create two log files that receive a specific requests

Best regards,

1 Answer

After updating your configuration files did you restart your nginx service so the change could take effect? service nginx restart

  • No I just reload the configuration service nginx reload . After that, there are no changes, but the reload command take effect when I remove the if statement.
    Nginx is on production, I will lose requests by restarting it.
    Do you think should I restart Nginx ?

    • if it's on production, pick a time where the load is low. If it's possible please use a test environment instead of your production one.

Have another answer? Share your knowledge.