Welcome! Log In Create A New Profile

Advanced

implementation of access.log with if condition

Posted by lifeisjustabout 
lifeisjustabout
implementation of access.log with if condition
July 31, 2017 11:40AM
Hi I would like to implement if condition on access and error log. Prior to
nginx restart I want nginx to look for first log path location eg.
/externalhdd/log/nginx/example.com_access.log, if log doesn't exist use
second path which is /var/log/nginx/example.com_access.log I have given
like my example. I guess I need some if conditions or something.

server {
listen 80;
server_name example.com;
root /var/www/example.com/public_html;
index index.php;
IF ACCESS LOG EXIST USE THIS PATH
access_log
/externalhdd/log/nginx/example.com/logs/example.com_access.log;
IF ACCESS LOG DOESN'T EXIST USE THIS PATH
access_log /var/log/nginx/example.com/logs/example.com_access.log;


error_log /var/www/example.com/logs/example.com_error.log error;

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

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,275761,275761#msg-275761

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Igor A. Ippolitov
Re: implementation of access.log with if condition
July 31, 2017 12:20PM
Hello,

As far as I know, it's impossible using Nginx.
But you can archive the same result using only 'access_log' statement
into 'permanent' location.
Just mount your external drive 'nginx' directory over /var/log/nginx like:
mount -o bind /externalhdd/log/nginx/ /var/log/nginx/

If it is unsuitable for you to 'hide' whole directory, you can create
a subdirectory for your logs and bind external hdd dir there:
mount -o bind /externalhdd/log/nginx/ /var/log/nginx/mysitelogs/

Hope this helps.

On 31.07.2017 12:35, lifeisjustabout wrote:
> Hi I would like to implement if condition on access and error log. Prior to
> nginx restart I want nginx to look for first log path location eg.
> /externalhdd/log/nginx/example.com_access.log, if log doesn't exist use
> second path which is /var/log/nginx/example.com_access.log I have given
> like my example. I guess I need some if conditions or something.
>
> server {
> listen 80;
> server_name example.com;
> root /var/www/example.com/public_html;
> index index.php;
> IF ACCESS LOG EXIST USE THIS PATH
> access_log
> /externalhdd/log/nginx/example.com/logs/example.com_access.log;
> IF ACCESS LOG DOESN'T EXIST USE THIS PATH
> access_log /var/log/nginx/example.com/logs/example.com_access.log;
>
>
> error_log /var/www/example.com/logs/example.com_error.log error;
>
> location / {
> index index.php index.html;
> }
>
> Posted at Nginx Forum: https://forum.nginx.org/read.php?2,275761,275761#msg-275761
>
> _______________________________________________
> nginx mailing list
> nginx@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
lifeisjustabout
Re: implementation of access.log with if condition
July 31, 2017 01:10PM
Thanks for the response. The reason why I want to implement this is because
I have dedicated server and thanks to nginx everything is work perfectly so
I don't need to check server everyday. I don't want to locate logs on ssd
main (hardrive /var/log/nginx/) which is obviously smaller hard disk than my
external sata hdd (/externalhdd/log/nginx/). I am afraid if something
happens to external hard drive time to time, I still want my website to
operate after restart because every week server restart by itself due to
some limitation of some programs. I guess it will give me error message
nginx couldn't found access.log if external hard disk dies prior to restart
nginx. I want to avoid this problem.

thanks.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,275761,275763#msg-275763

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Lucas Rolff
Re: implementation of access.log with if condition
July 31, 2017 02:30PM
> I don't want to locate logs on ssd main (hardrive /var/log/nginx/) which is obviously smaller hard disk than my external sata hdd (/externalhdd/log/nginx/

Setup log rotation depending on size of logs or daily rotation.

> I still want my website to operate after restart because every week server restart by itself due to some limitation of some programs

Instead of randomly restarting – do a config check and only restart if the config is valid (should also validate the log location)

You write in your initial post that if the access log exists, then you should use the external harddrive, however – have you considered that the file might exist but the device is in read-only?

You could modify your init script or systemd start script to actually check for these things and generate a new nginx config depending on the result (hint: use place holders).

You could also symlink /externalhdd/log/nginx to /var/log/nginx – in this case if the drive is mounted it would use the external drive, if it’s not mounted the file would get written to your SSD – but still won’t make up for a read only device in case that happens.

To be honest, log rotation or symlinks would IMO be the best options.

On 31/07/2017, 13.08, "nginx on behalf of lifeisjustabout" <nginx-bounces@nginx.org on behalf of nginx-forum@forum.nginx.org> wrote:

Thanks for the response. The reason why I want to implement this is because
I have dedicated server and thanks to nginx everything is work perfectly so
I don't need to check server everyday. I don't want to locate logs on ssd
main (hardrive /var/log/nginx/) which is obviously smaller hard disk than my
external sata hdd (/externalhdd/log/nginx/). I am afraid if something
happens to external hard drive time to time, I still want my website to
operate after restart because every week server restart by itself due to
some limitation of some programs. I guess it will give me error message
nginx couldn't found access.log if external hard disk dies prior to restart
nginx. I want to avoid this problem.

thanks.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,275761,275763#msg-275763

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
lifeisjustabout
Re: implementation of access.log with if condition
July 31, 2017 04:40PM
Thanks for response,

> Setup log rotation depending on size of logs or daily rotation.
If I use log rotation would I still have access to all access data logs
where I moved?

>Instead of randomly restarting – do a config check and only restart if the
config is valid (should also validate the log location)
There is a scheduled script restarts server to close some program sockets
once every week. Sorry I didn't explain correctly.

>You write in your initial post that if the access log exists, then you
should use the external hard drive, however – have you considered that the
file might exist but the device is in read-only?
I never had problem before on this servers external hdd In terms of device
never become read only for last 3 years.

>You could also symlink /externalhdd/log/nginx to /var/log/nginx – in this
case if the drive is mounted it would use the external drive, if it’s not
mounted the file would get written to your SSD – but still won’t make up for
a read only device in case that happens.
My friend is also suggested symblink, I will look on this suggestion

thank you.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,275761,275770#msg-275770

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login