I compiled the nginx 'echo-nginx-module' module to log the request body.
My goal is to log and reverse proxy all the traffic coming to nginx to another DNS.
I am using proxy_pass to redirect the traffic to the second DNS and it successfully started to log the traffic body.
However, what's missing is I also need to log the traffic header and timestamp.
I've added the lines below to the proxy_pass config file and it does not seem to log headers. What am I missing?
location / {
# the below four lines do not log header and body
echo "headers are:"
echo $echo_client_request_headers;
echo_read_request_body;
echo $request_body;
# this works and logs traffic envelope
proxy_pass https://offexserver-test-internal.leapaws.com.au;
root html;
index index.html index.htm;
}
You should be doing something like this
http {
log_format custom '$request_body';
server {
server_name default_server;
listen 80;
location / {
echo_read_request_body;
}
}
}
You cannot log all the headers, the only thing you can do is, specify the headers individually you want to log. or you will have to use an additional module to configure headers.
Related
I'm trying to use Nginx as a proxy server which should redirect all requests to different domains based on the URI parameters.
Say we have two servers org1-domain.com and org2-domain.com. The incoming request is mynginxproxy.com/api/org2/users. In this case, Nginx should proxy the request to the org2-domain.com.
Here's my Nginx config file:
user nginx;
worker_processes 1;
events {
worker_connections 10240;
}
http {
server {
listen 8080 ssl;
server_name nginx-proxy;
location ^/\w*/(.*)/.*$ {
proxy_pass https://$1-domain.com:8080;
}
}
}
So I'm using a regex in the location directive in order to get org parameter from the URI and use it in the proxy_pass directive. But I'm always getting this error:
host not found in upstream "-domain.com" in /etc/nginx/nginx.conf:15
I also tried other options for regex in the location directive:
location ~ ^/\w*/\w*/(.*)/(.*)$ {
location ~* /(.*)$ {
location ~* /(.*)$ {
But in all those cases I'm always getting the same host not found error.
I also tried to use rewrite rules instead of proxy_pass but in this case, Nginx just returns me a 302 redirect response which is not suitable for my case.
BTW proxy_pass without regex works fine if I'm redirecting directly to the org2-domain.com:
location / {
proxy_pass https://org2-domain.com:8080;
}
But I need somehow to extract org from the URI and construct the DNS name for proxy_pass directive.
My initial NGINX load balancer configuration was pretty simple:
upstream myapp {
server 10.11.12.13:80; #server01
server 10.11.12.14:80; #server02
}
server {
listen 80;
server_name localhost;
location /myapp/ {
proxy_pass http://myapp;
Let's say the localhost has the IP 1.2.3.4.
Result:
The user calls 1.2.3.4/myapp and gets redirected to one of those two servers including the requested filepath.
For example: 1.2.3.4/myapp/results gets redirected to maybe 10.11.12.13/myapp/results.
Now I have ONE special case to include, this is where I struggle. ALL requests should still be handled exactly the same with this one exception:
If 1.2.3.4/specialFilePath is called I want to redirect to a totally different, static URL e.g. externalPage.com.
Can I add this case somehow to my Nginx configuration?
You could add a second location block in which you defile what to do with the specialFilePath like
location /specialFilePath {
proxy_pass http://externalservice.com;
}
Then check the configuration with nginx -t or sudo nginx -t and reload the configuration
I have a nginx server that runs drupal. We're changing the way our site runs. I want user.redangus.org to pull up the same page that is at redangus.org/user.
My nginx right now is resulting in a redirect to redangus.org/user, but I want the URL to stay as user.redangus.org
My nginx for this currently looks like:
# user.redangus.org
server {
listen 80;
server_name user.redangus.org;
location / {
proxy_pass http://127.0.0.1/user$request_uri;
proxy_set_header Host redangus.org;
}
}
How do I get it to pull the page without redirecting to the page?
I've installed minicron from https://jamesrwhite.github.io/minicron/ on an ubuntu aws server with Nginx and when I don't specify a directory in location, the primary url is redirected to the Cron page as expected. However, if I add 'minicron' to the location as you see below, I get "Sinatra doesn't know this ditty" error. I'm at a lost and google hasn't been any help for either of these problems. My main goal is just to get minicron to load at a separate sub domain or directory than the main url. Any help would be appreciated.
http{
...
server {
# The port you want minicron to be available, with nginx port 80
# is implicit but it's left here for demonstration purposes
listen 80;
# The host you want minicron to available at
server_name *.com;
location /minicron {
# Pass the real ip address of the user to minicron
proxy_set_header X-Real-IP $remote_addr;
# minicron defaults to running on port 9292, if you change
# this you also need to change your minicron.toml config
proxy_pass http://127.0.0.1:9292;
}
}
}
Currently the URI /minicron is being sent upstream to http://127.0.0.1:9292/minicron. Your previous test would have hit http://127.0.0.1:9292/.
You need to rewrite the URI before sending it upstream. Maybe:
location /minicron/ {
...
proxy_pass http://127.0.0.1:9292/;
}
location = /minicron { rewrite ^ /minicron/ last; }
See this and this for details.
I've been setting up Nginx as a reverse proxy for an app on the server. Part of this includes a maintenance page that has external content (like images). I was able to find a method for setting up an error page with images returning 200, but it looks like a reverse proxy will change the whole environment. Here's the original solution from nginx maintenance page with content issue
error_page 503 #maintenance;
location #maintenance {
root /path_to_static_root;
if (!-f $request_filename) {
rewrite ^(.*)$ /rest_of_path/maintenance.html break;
}
return 200;
}
The Reverse Proxy is configured as:
location / {
proxy_pass http://127.0.0.1:9007/;
proxy_redirect off;
}
The Problem is that when a file is found to exist in the "maintenance" root, something goes wrong and the server returns a 502. Anyone know what the cause could be?
Some speculation I'm wondering if server listens on port 80, it somehow passes any good file request back into the proxy. If that were true, how would that be avoided?
Edit
Here's the error in the nginx log. I am directly trying to access 50x.html. Not sure why this would occur?
2012/02/17 19:39:15 [error] 21394#0: *13 connect() failed (111: Connection refused) while connecting to upstream, client: (my ip address), server: _, request: "GET /50x.html HTTP/1.1", upstream: "http://127.0.0.1:9007/50x.html", host: "domain.com"
It looks like it is indeed trying to GET from the app and not the root. How can I bypass this?
Edit 2
I originally thought I had found an answer where a change was made for nginx v1.0.12 but it did not solve the problem. It involved a similar situation but my guess is the fix was too specific.
You shouldn't need to involve the backend (I.E., shouldn't use proxy pass) since your maintenance page should be a static html file that Nginx can serve directly.
Assuming you have a setup configured as ...
server {
listen 80;
server_name example.com;
root /path/to/webroot;
# Regular locations etc
...
}
Create a folder called "503_status" and put your maintenance page in there as "503.html".
With that in place, create a file called "maintenance.default" under the Nginx directory with the following content ...
error_page 503 /503_status/503.html;
# Candidate for redirection if not ending with one of these extensions.
if ( $request_uri !~ \.(jpg|gif|png|css|js)$ ) {
set $maint "Tr";
}
# Candidate for redirection if not a request for the maintenance page
if ( $request_uri !~ ^/maintenance/$ ) {
set $maint "${maint}ue";
}
# Redirect to a location where the status code will be issued
if ( $maint = True ) {
rewrite ^ /maintenance/ redirect;
}
# Due to Nginx quirk, issue the status code in a location context.
# Make "internal" to prevent direct browsing.
location /maintenance {
internal;
return 503;
}
# 503_status folder as "internal" so no direct browsing
location 503_status {
internal;
alias /server/path/to/503_status/folder;
}
Whenever you put to put the site into maintenance, just include the file as follows ...
server {
listen 80;
server_name example.com;
root /path/to/webroot;
include /server/path/to/maintenance.default;
# Regular locations etc
...
}
This will serve your maintenance page as well as any resources it needs (just make sure extension is in the list). The backend server does not come into play at all.