NGINX Log request on specific URI - nginx

I want to log all request coming from a specific URI. For example, I want to log all request coming from http://test123.com/api/xxxxxxx, with /api.
server {
listen 80;
server_name xxxxxxx;
root /usr/share/nginx/app/public;
index index.php index.html;
location /api{
access_log /var/log/nginx/test.access.log main;
}
}
This code successfully logs the request but returned an error
open() "/usr/share/nginx/app/public/api" failed (2: No such file or directory)
Thank you so much.

location /api is that location exist file and dir. Please check properly files.

Related

Nginx Proxy Pass dont acceppts user & password

i need to proxy pass one URL from my internal network, that looks like this here:
http://<user><passwd><ipaddress>:<port>/cgi-bin/mjpg/video.cgi?channel=1&subtype=1
But this configuration always ends up in an error:
nginx: [emerg] invalid port in upstream "user:passwd#192.168.133.122:8080/cgi-bin/mjpg/video.cgi?channel=1&subtype=1" in /etc/nginx/sites-enabled/default:15
nginx: configuration file /etc/nginx/nginx.conf test failed
The only working way i get nginx to work is this one:
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
location /video.cgi {
proxy_pass http://192.168.133.122:8080/cgi-bin/mjpg/video.cgi?channel=1&subtype=1;
}
}
But in this configuration the User and the Password are not included. Is there a way to get user&password also in the proxy_pass?
thanks
Franz

Nginx reverse proxy return 502

I'm very new to nginx and server game and i'm trying to setup a reverse proxy. Basically what i need is when i enter my server ip it should open a particular website (Ex: https://example.com).
So for example if i enter my ip (Ex: 45.10.127.942) it should open the website example.com , but the url should remain as http://45.10.127.942.
I tried to set my server configuration as follows but it returns a 502 error.
server {
listen 80;
location / {
proxy_pass http://example.com;
}
}
It returns a 502 error. Can you please explain what i need to do?
You can have something like this in your configuration file:
server {
root /var/www/html;
server_name _;
location / {
try_files $uri $uri/ /index.html;
}
}
Place the index.html file in root folder specified.
Then just restart the NGINX and it should work.
What is the problem with your configuration file is you should not proxy_pass.
If you want to open the other website, you should have DNS record pointing to that IP. What actually happens is the thing you are trying to do is known as CLICKJACKING. For more details, search CLICKJACKING on google and you will find a lot of references.

nginx location directive doesn't look for specified file first

I have a simple nginx configuration file -
server {
listen 80 default_server;
root /var/www/example.com;
#
# Routing
#
location / { index index.html; }
location /foo { index foo.html }
#
# Logging
#
access_log /var/log/nginx/{{ www_domain }}.log;
error_log /var/log/nginx/{{ www_domain }}-error.log error;
server_name example.com;
charset utf-8;
}
As you can see, there's only 2 routes - the / and /foo paths.
When I go to www.example.com/ all works correctly. I can see the index.html page being served.
When I go to www.example.com/foo, I get a 404 error when I should be getting the foo.html page.
Looking at the logs, I see this error:
2018/08/13 21:51:42 [error] 14594#14594: *6 open() "/var/www/example.com/foo" failed (2: No such file or directory), client: XX.XX.XX.XX, server: example.com, request: "GET /foo HTTP/1.1", host: "example.com"
The error implies that it's looking for a file named /var/www/example.com/foo and not /var/www/example.com/foo.html like I would expect.
Why does this happen in general, and specifically why does it not happen on my root path / ?
Thanks!
Edit: It does work if I visit www.example.com/foo.html directly
The index directive will append the filename, when you give the URI of a directory.
So / points to the root directory (/var/www/example.com/), and the index index.html; statement causes nginx to return the file /var/www/example.com/index.html.
The /foo URI does not point to a directory. If the directory /var/www/example.com/foo/) did in fact exist, the index foo.html; statement would cause nginx to return the file /var/www/example.com/foo/foo.html. Which is not /var/www/example.com/foo.html.
What you are attempting to achieve is some kind of extension-less scheme, which is nothing to do with the index directive.
See this document for details of the index directive.
There are many solutions that will work, for example, using try_files instead of index:
location /foo { try_files /foo.html =404; }
See this document for details.

HTTP user agent block nginx restart fail

I trying to paste the following on my
nginx version: nginx/1.4.6 (Ubuntu)
server {
server_name www.example.com example.com;
access_log /var/www/logs/example_access.log;
error_log /var/www/logs/example_error.log;
root /var/www/html;
# case insensitive matching
if ($http_user_agent ~* (netcrawl|npbot|malicious|wget)) {
return 403;
}
location / {
index index.html index.htm index.php;
}
}
service nginx reload && service nginx restart
I did the following at another server
wget "http://mymainserver.com/myfile.html"
It still able to 200 ok fetch the file.
Any idea what do i do wrong.
Thanks!
Missing "}" in your config file
nginx: [emerg] unexpected end of file, expecting "}"
As a result,
nginx reload fails and service nginx restart is not even not called.
OR
server_name in your config file mismatches hostname used in wget => nginx skips your location

stuck on nginx location directive

I am working on windows and using Winginx, modifying the nginx.conf file under conf folder. I have changed up the server block listening to port as follows:
server{
listen 127.0.0.1:80;
log_not_found off;
charset utf-8;
access_log logs/access.log main;
location /images/ {
root home/localhost/public_html;
index index.php index.html;
}
}
The images folder is under the home folder in winginx and is added as a domain through the hostseditor. However, when I use the URL http://images, I get the 404 error.
If the location directive is changed to:
location / {
}
Everything works fine then. Neither do any regular expressions work except if I match the '/'. I have read up on location directive but could not find any relevant reason. Would be great help if anyone could point out the error. Thanks.
The reason is that the location directive matches URI (absolute path of the Request-URI to be more precise), not Host.
When you use http://images, your browser sends request like this:
GET / HTTP/1.1
Host: images
Notice that request URI is /.

Resources