Logs are really one of the essential things for making sure your web app or api or anything that you have deployed is performing well and also to find out whether it is having any production level bugs.
I like to log the errors of my softwares that I make whether it is an api then I log it inside a file and if it is a flutter app then I use firebase crashlytics.
Any software that I make I make sure to turn on the logs especially the error logs because I know am not perfect. So I strive to be one by fixing every mistake and bug that I made.
So now if you want to view the logs of nginx. It is really simple and easy to view.
If you want to view the access logs (Access logs are the ip address and the browser of all the requests that your nginx server has recieved. It has no specific condition to show up here.)
cat /var/log/nginx/access.log
But if you want to see all your error logs, Error here means the route that has emitted a response code of above 400.
cat /var/log/nginx/error.log
But before that you should have enabled logging in the first place to view all the logs.
Installing Nginx
Let me help you from the beginning of installing nginx to setting up logs.
If you are using ubuntu or any debian based operating system.
It is really simple to install.
sudo apt-get update && sudo apt-get install nginx -y
This should update your repositories and install nginx and run it in the background and you can check it by going in to the chrome and type the url of the server or raspberry pi if you use like me.
It should show something like welcome to nginx.
Running nginx in the background
So if you have just installed now, then this step is unnecessary but if you have already installed nginx and you have either rebooted your device or you have switched off the device accidentally.
Then you have to start the nginx in order for it to work on the background.
It is also simple to do.
sudo systemctl start nginx
If you are using really way older version of linux then use this command.
sudo systemd start nginx
Now that's out of our way.
You have to also check whether it is running then check that the device ip is showing welcome to nginx.
Or you can run this command.
sudo systemctl status nginx
It should show active just like in this image.
Configuring Nginx Configuration File
This is step differs for everyone and it depends on the software that you are trying to deploy.
If you don't know how to use nginx at all then follow my series on nginx in this blog. Click Here.
Right now I am going to enable reverse pass to port 3000 and reload the nginx.
Type this command for editting the file
sudo nano /etc/nginx/nginx.conf
Delete everything and then we have to copy paste the below code for reverse proxying it.
events {} http { server { listen 80; location / { proxy_pass localhost:3000; } } }
If you don't understand this at all then go through the series. I have made a lot of posts on nginx. Click Here.
That's all for the nginx setup.
Enabling Logging In Nginx
To enable nginx there are two ways to do it, first way is for a single logging system for all your nginx applications (sucks for more than one web app.) Second way is a logging for every seperate server block (A little more work).
If you only want a single logging system for all your web apps in your nginx (not recommended for more than one apps in nginx.)
It is simple to do.
events {} http { access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; server { listen 80; location / { proxy_pass localhost:3000; } } }
This will enable logging for all your applications in a single file.
Enable logging seperately for every single server block.
events {} http { access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; server { access_log /var/log/nginx/myapp.log; error_log /var/log/nginx/myapp-error.log; listen 80; location / { proxy_pass localhost:3000; } } }
Add this two more lines in the server block and change the name of the file.
Perfectly done then.
Viewing logs from nginx.
It is really simple to do just type the below code in your terminal.
If you want to view the access logs (Access logs are the ip address and the browser of all the requests that your nginx server has recieved. It has no specific condition to show up here.)
cat /var/log/nginx/access.log
But if you want to see all your error logs, Error here means the route that has emitted a response code of above 400.
cat /var/log/nginx/error.log
Here is an image from one of my servers.
I know that looks too crappy but you have to export it to your pc for analysing it. Surely will meet you back with another post on how to analyze nginx logs.
Conclusion
So finally we have setupped your server to log the views that it recieves and also how to view it.
So, if you are wondering to learn more about programming then visit the home page of this blog and you will find superb and posts that would be useful for you.
Guys, one more thing I am sorry for not posting for the previous week I got a little caught up with a little work and I am in the verge of releasing my own app.
It took me four days to make and I was doing that as a challenge that's why I wasn't able to post anything