Nginx duplicate log_format name timed_combined error - web-services

I am getting this error:
Restarting nginx: nginx: [emerg] duplicate "log_format" name "timed_combined" in /etc/nginx/sites-enabled/default:8
nginx: configuration file /etc/nginx/nginx.conf test failed
whenever I am tring to start or restart my nginx server. This does not happen before. Here's the first few lines of code in my /etc/nginx/sites-enabled/default
# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts
log_format timed_combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time';

The "timed_combined" log_format is predefined in sources; you need to use your name, something like:
log_format my_log '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time';
After that you need to re-define access_log:
access_log /path/to/access.log my_log

Related

nginx: [emerg] invalid number of arguments in "include" directive

I am learning how to deploy AWS for the first time. I am following this guide here: https://www.youtube.com/watch?v=HtWgb_vbyvY.
I am using windows machine while the person in the video uses mac.
I am getting the following error when running ngnix commands on the windows terminal: "nginx: [emerg] CreateFile() "C:/nginx-1.23.0/nginx-1.23.0/mime.types" failed (3: The system cannot find the path specified) in C:\Users\Shi Jie\Downloads\nginx-1.23.0\nginx-1.23.0/conf/nginx.conf:12"
i think it is how i write the path on my ngnix.conf which i write as such
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include \Users\Shi Jie\Downloads\nginx-1.23.0\nginx-1.23.0\mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
client_body_buffer_size 100k;
client_header_buffer_size 1k;
client_max_body_size 100k;
large_client_header_buffers 2 1k;
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 5 5;
send_timeout 10;
server_tokens off;
#gzip on; on;
include /etc/nginx/conf.d/*.conf;
}
Can anyone point the way to teach me how to write the path properly? Thank you.
You need to escape spaces, { and ". So change your include line to the following:
...
include /Users/Shi\ Jie/Downloads/nginx-1.23.0/nginx-1.23.0/mime.types;
...

Write response requests on logs with Nginx

!UPDATE!
Big thank's to #ivan
default.conf:
log_format include_id '$remote_addr - $remote_user [$time_local] $request_id "$request" '
'$status $body_bytes_sent "$http_referer" "$http_user_agent"';
server {
listen 443;
listen [::]:443;
ssl_certificate /etc/nginx/conf.d/cert.pem;
ssl_certificate_key /etc/nginx/conf.d/key.pem;
location / {
proxy_pass http://sns-mock:9911;
proxy_store /var/log/nginx/requests/mohsin.json;
}
}
docker-compose.yml:
version: '3.7'
services:
sns-mock:
image: s12v/sns
container_name: sns-mock
ports:
- "9911:9911"
networks:
- default-nw
volumes:
- ./config/db.json:/etc/sns/db.json
sns-mock-proxy:
image: nginx:latest
container_name: sns-mock-proxy
ports:
- "8443:443"
networks:
- default-nw
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./log/nginx:/var/log/nginx
- ./log/nginx/requests:/var/log/nginx/requests
depends_on:
- sns-mock
networks:
default-nw:
external: true
Php test file:
<?php
use Aws\Result;
use Aws\Sns\SnsClient;
use Illuminate\Http\JsonResponse;
use PHPUnit\Framework\TestCase;
class FooTest extends TestCase
{
public function testFoo()
{
$snsClient = new SnsClient([
'endpoint' => 'localhost:8443',
'region' => 'eu-west-2',
'version' => 'latest'
]);
$result = $snsClient->publish([
'Message' => 'foo',
'TopicArn' => 'arn:aws:sns:eu-west-2:123450000001:test-topic'
]);
dd($result, 'ok');
}
}
Test result:
My project:
On my access.log file, i can catch response from phpunit test like here:
172.28.0.1 - - [23/May/2022:06:54:04 +0000] "POST / HTTP/1.1" 200 270 "-" "aws-sdk-php/3.222.17 OS/Linux/5.13.0-41-generic lang/php/8.1.6 GuzzleHttp/7" "-"
===================================================================================================================================
The general answer to your question is "No". However some kind of workaround is possible.
Every request gets processed by nginx receives some internal request ID available via the $request_id variable (16 random bytes, in hexadecimal). You can add that ID to your access log defining your own custom log format:
log_format include_id '$remote_addr - $remote_user [$time_local] $request_id "$request" '
'$status $body_bytes_sent "$http_referer" "$http_user_agent"';
access_log /var/log/nginx/access.log include_id;
Next, in the same location where you have your proxy_pass directive add the proxy_store one:
proxy_store /var/log/nginx/requests/$request_id.json;
I use the separate /var/log/nginx/requests directory here to not make your /var/log/nginx turning into a mess. Of course it should be created manually before you start the docker container, and it should be writable the same way as /var/log/nginx itself (including such a things as SELinux context, if being used on the host system). However for the testing purposes you can start with the proxy_store /var/log/nginx/$request_id.json;.
The whole nginx config should look like
log_format include_id '$remote_addr - $remote_user [$time_local] $request_id "$request" '
'$status $body_bytes_sent "$http_referer" "$http_user_agent"';
server {
listen 443;
listen [::]:443;
access_log /var/log/nginx/access.log include_id;
ssl_certificate /etc/nginx/conf.d/cert.pem;
ssl_certificate_key /etc/nginx/conf.d/key.pem;
location / {
proxy_pass http://sns-mock:9911;
proxy_store /var/log/nginx/requests/$request_id.json;
}
}
If you want requests to be available via the plain HTTP protocol too, you can use the same configuration for the plain HTTP server block instead of the one you've shown in your question:
server {
listen 80;
listen [::]:80;
access_log /var/log/nginx/access.log include_id;
location / {
proxy_pass http://sns-mock:9911;
proxy_store /var/log/nginx/requests/$request_id.json;
}
}
or issue an HTTP-to-HTTPS redirect instead:
server {
listen 80;
listen [::]:80;
return 308 https://$host$request_uri;
}
Now the response body for each request which can be identified from the access log via its request ID, something like
172.18.0.1 - - [20/May/2022:19:54:14 +0000] d6010d713b2dce3cd2713f1ea178e140 "POST / HTTP/1.1" 200 615 "-" "aws-sdk-php/3.222.17 OS/Linux/5.13.0-41-generic lang/php/8.1.6 GuzzleHttp/7" "-"
will be available under the /var/log/nginx/requests directory (response body for the request shown in the given access log entry will be available as d6010d713b2dce3cd2713f1ea178e140.json file).

NGINX Logging on AWS with ec2-user is not happening

Below is my edited nginx.conf
user ec2-user;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 2048;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#tcp_nodelay on;
keepalive_timeout 65;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
}
When I changed the user variable from nginx user to ec2-user, the logs are not getting captured in the /var/log/nginx/access.log file even though the file is present it always remains zero size

NGINX gives 502 error from browsers but not from Postman

I've got a Django application running on Azure App Service using NGINX.
My nginx.conf file is as follow:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
fastcgi_max_temp_file_size 0;
fastcgi_buffers 128 2048k;
fastcgi_buffer_size 2048k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
server {
listen 8000;
location / {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
location /static {
alias /app/staticfiles;
}
}
}
daemon off;
Everything works fine, except for one particular API where I include in the header some token (typical bearer token) and it's returning a 502 error from Chrome (in the network tab)
However, when I try to call this from Postman, it's returning the data correctly.
What could be possibly wrong here?
Thanks to #Selcuk's suggestion. I've managed to fix the above error by increasing the buffer-size in iwsgi.ini file
# uwsgi.ini file
buffer-size = 32768

Nginx routing Django Web App links to wrong path

I have a django app where the hompage routes correctly, but when I try to click on any of the links I get a 404 error. I looked at the logs and see:
*1 open() "/usr/share/nginx/html/pricing" failed (2: No such file or directory)
which tells me that nginx is not looking at my project folder but instead what seems to be a default setting. I am using centos, so I had to manually setup sites-available and sites-enabled, which I have used to get the homepage working. As such there is no default conf to disable. I am unsure how to get nginx to route to my path instead of the default. My nginx.conf file looks like this:
user so_dev;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*; #added by me
}
I followed a bad tutorial which had a typo in it. If you happen to follow same type, inside your nginx sites-available file you should put:
location /
and NOT
location = /
https://www.shellvoide.com/hacks/installing-django-application-with-nginx-mysql-and-gunicorn-on-ubuntu-vps/