Nginx rewrites parent rules with same key in sub-files? - linux

Accessing the /etc/nginx/nginx.conf file I have the following rule:
server {}
http {
access_log logs/access.log main;
error_log logs/error.log error;
include /etc/nginx/conf.d/*.conf;
In another file, called etc/nginx/conf.d/custom.conf, I have the code:
http {
blablabla …
When NGINX loads the settings, will the HTTP code block that was inside NGINX.CONF be “merged” with the settings included below or will it simply be replaced?
In this scenario for example, will the LOGS continue to be saved in the folder even if CUSTOM.CONF doesn't implement anything about it?
My current issue:
I can't see the logs that should be printed in the error/access.log files, remembering that my application is generating a 500 HTTP error.

Even if you can have more then a single http block you should not do it. But having an http block in another http block IS NOT ALLOWED and will raise an error on startup. Just tested this on my instance. So you should check the output of nginx -T and check the configuration.
The NGINX configuration works in an hierarchical order. Means a directive you set in the http context can be overwritten in the server or location context if supported.

Related

sub URLs to my website not loading on Linux, Azure and Nginx - 404 not found [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 11 months ago.
Improve this question
Can someone please help on what the issue is here.
I have a React web app hosted on Azure Web App. It has been deployed successful.
My problem is, if, for example, I go to the site on myweb.azurewebsites.net it loads well. And if I click on a button like the "signin" button it redirects me to myweb.azurewebsites.net/signin.
However, if I manually type in myweb.azurewebsites.net/signin into the browser, it gives an "Nginx not found" error.
Can someone help on what the issue is. I suspects it's between Azure and/or Nginx.
Nginx not found 404 error means Nginx can’t find the resources your web browser asks for.
Check web root directory exists on your server.
Make sure your website files are stored in the correct directory.
To resolve "Nginx not found 404 error", please find below workarounds if helpful:
Workaround1:
The most likely issue is that you're not telling Nginx to forward
other requests to the /index.html of your application, which makes it
so your other pages can't be loaded directly and display a 404 error.
To fix this, you'll need to make a small change to your Nginx
configuration files.
Make sure your nginx.conf file as shown below:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root /home/user_name/Documents/your_proj; # You need to provide here the path of your "index.html" file
index index.html index.htm;
}
}
Workaround2:
If Workaround1 does not work, try changing the location segment as below
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
Workaround3:
Configure the startup command on "Settings > General settings > Startup Command".
Change the path to your build path.
To redirect all queries, use the --spa option:
pm2 serve /home/site/wwwroot/client/build --no-daemon --spa
Workaround4:
In the nginx.conf file, try commenting-out the disable_symlinks on line or changing it to disable_symlinks off.
I discovered that I didn't add an Nginx conf file and copy it into the Nginx Docker container. When I did that, it works fine.

Nginx fails to receive custom header when Redirected by Proxy server Node

I am running an NGINX server pointing to my proxy server which is running on Node. I am redirecting to app/ from node using express redirect. I am also passing a custom header using res.set("X-Custom-Header","XXX"); before the redirect. But the same header is not retrieved on app/ route on NGINX. Is there anything I am missing?
By default, nginx does not pass headers containing underscores.
Try:
underscores_in_headers on;
See this document for details.

nginx responding "301 moved permanently"

Consider the following nginx config file:
server {
listen 443;
ssl on;
ssl_certificate /etc/tls/cert.pem;
ssl_certificate_key /etc/tls/key.pem;
location / {
proxy_pass http://api.default.svc.cluster.local;
}
}
All incoming TCP requests on 443 should redirect to my server running on api.default.svc.cluster.local:80 (which is a node REST-Api btw). This works fine, I can curl https://<nginx-IP>/ nginx and get a correct response, as expected.
Now, I'd like to change the location from / to /api, so I can fire a curl https://<nginx-IP>/api in order to get the same response as before.
1. Attempt
So I change the location line in the config to:
location /api {
Unfortunately this won't work, instead I get an error Cannot GET /api which is a node error, so obviously it gets routed to the api but something's still smelly.
2. Attempt
It seems as the trailing slash in an URI is required so I added it to the location:
location /api/ {
Now something changed. I won't get the same error as before, instead I get an "301 moved permanently". How can I fix my nginx config file?
Additional information regarding the environment
I'm using a kubernetes deployment that deploys the nginx reverse proxy incl. the config introduced. I then expose nginx using a kubernetes service. Also, I tried using kubernetes ingress to deal with this situation, using the same routes, however, the ingress service would respond with a default backend - 404 message.
As mentioned in the question, trailing slashes in URIs are important. I fixed this in the location, however, I didn't add it to the URI I pass using proxy_pass.
As for the nginx proxy I got it to work using the following config:
server {
listen 443;
ssl on;
ssl_certificate /etc/tls/cert.pem;
ssl_certificate_key /etc/tls/key.pem;
location /api/ {
proxy_pass http://api.default.svc.cluster.local/;
}
}
Concerning the ingress solution, I was not able to get it to work by adding the missing trailing slash to the path. The service is specified due its name and therefore no trailing slash can be added (i.e. it would result in an error).

Need help on Nginx CGI Configuration

I need to set the nginx configurations such that the URL "http://host/cgi-bin/hw.sh/some/path/to/data/" should trigger the shell script "hw.sh" present under path "/usr/lib/cgi-bin/".
Now, according to the instructions mentioned in page https://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-debian-squeeze-ubuntu-11.04-p3, we need to set the configurations under a ".vhost" file. But I have a default file already present under path "/etc/nginx/sites-available/default" instead of a .vhost file.
And when I use the same configurations, I get HTTP/1.1 403 Forbidden error. I have made sure that the script has required executable rights too. Below is the error received in nginx logs.
FastCGI sent in stderr: "Cannot get script name, are DOCUMENT_ROOT and SCRIPT_NAME (or SCRIPT_FILENAME) set and is the script executable?"
while reading response header from upstream,
client: host_ip, server: localhost,
request: "HEAD /cgi-bin/hw.sh/some/path/to/data/ HTTP/1.1",
upstream: "fastcgi://unix:/var/run/fcgiwrap.socket:", host: "host_ip"
I need help in writing the correct configuration so that my URL above executes the hw.sh script under the path mentioned above and returns proper output. Could someone please help me out here?
Below is my configuration used under default file.
server {
listen 80 default_server;
[...]
location /cgi-bin/ {
# Disable gzip (it makes scripts feel slower since they have to complete
# before getting gzipped)
gzip off;
# Set the root to /usr/lib (inside this location this means that we are
# giving access to the files under /usr/lib/cgi-bin)
root /usr/lib;
# Fastcgi socket
fastcgi_pass unix:/var/run/fcgiwrap.socket;
# Fastcgi parameters, include the standard ones
include /etc/nginx/fastcgi_params;
# Adjust non standard parameters (SCRIPT_FILENAME)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
[...]
}
Line "fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;" in the configuration was causing the problem.
When i changed it to "fastcgi_param SCRIPT_FILENAME $request_filename;", everything is working as expected.

Serving index.html displays "Internal Error: Missing Template ERR_CONNECT_FAIL" in browser

I'm trying to get my Node server up and running on Ubuntu 14.04. I followed a tutorial from DigitalOcean to set up nginx and server blocks to serve my content.
I have the server setup correctly, I believe because I can whois my-site.com and also ping my-site.com. when I visit the web address in the browser, however I get just this error that displays in the page: "Internal Error: Missing Template ERR_CONNECT_FAIL".
I thought that maybe I pointed the nginx server block to the incorrect path, because of of the "Missing Template", but it points to the right file. It is supposed to display a simple index.html file located in /var/www/my-site.com/html.
Here is my server block if this sheds some light on the error:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=off;
root /var/www/my-site.com/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name my-site.com www.my-site.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
This file is located in /etc/nginx/sites-available/my-site.com and I've copied it to the sites-enabled directory as well.
What am I missing here?
This is a pretty standard error message, and in fact as of this moment nodejs.org is displaying that exact same message. I believe it is generated by a reverse proxy: for example, https://searchcode.com/?q=ERR_CONNECT_FAIL shows that ERR_CONNECT_FAIL appears in the squid reverse proxy software. I couldn't find something similar a quick search through the nginx source code.
When I encountered this error message, I was deploying through the digitalocean one-click dokku app and I did not have a domain in /home/dokku/VHOST, so it was being assigned a random internal IP address. I accessed it using [domain]:[port]. Hope that gives you a clue.

Resources