Nginx saves it’s logs, configs, binaries in different folder in different operating system. Every time I struggle to find them in different locations.
Also, the commands are different. I have curated these in this document based on Ubuntu, Macbook and CentOS(coming soon).
Moreover, there are some frequently encountered issues and solution.
Root config file location
Mac: /usr/local/etc/nginx/nginx.conf
Ubuntu: /etc/nginx/nginx.conf
Log file location
You may need to uncomment a line in the config file to actually get error dump in the file. You may wish to keep it clean periodically.
Mac: /usr/local/Cellar/nginx/1.23.2/logs/error.log
Ubuntu: access_log /var/log/nginx/access.log
error_log /var/log/nginx/error.log
Commands
Test config files
Mac: sudo nginx -t
Reload config files
Mac: sudo nginx -s reload
Linux: sudo systemctl reload nginx
sudo service nginx reload
Stop nginx
Mac: sudo nginx -s stop
Ubuntu: sudo systemctl start nginx
Start nginx
Mac: sudo nginx
Linux: sudo systemctl start nginx
Restart nginx
Linux: sudo systemctl restart nginx
sudo /etc/init.d/nginx restart
Troubleshooting
upstream sent too big header while reading response header from upstream
It occurs in a reverse proxy block. To fix this issue, find the reverse proxy block and add the following statements.
1 | location / { |
Rewrite based on a part of URL
I actually don’t try to use the rewrite
statement until it’s really necessary. Here are some examples, that can be achieved using rewrite
and regex based location block both.
1 | location ~ /server1/(.*)$ { |
Rewrite
1 | rewrite regex URL [flag]; |
Using rewrite
URL(not tested),
1 | location /server1/ { |
Above blocks can be merged, will update it later.
Above config sends http://example.com/server1/robin
request to a service running in the 3050 port, like LOCALHOST:3050/robin
.
Also, sends http://example.com/server2/robin
request to a service running in the 3052 port, like LOCALHOST:3052/robin
.
Note: It’s not redirection.