nginx not caching response from reverse proxy - nginx

http://nginx.org/en/docs/http/ngx_http_memcached_module.html
Basic config is here:
worker_processes 2;
events {
worker_connections 1024;
}
error_log /var/log/nginx/nginx_error.log warn;
error_log /var/log/nginx/nginx_error.log info;
http {
upstream backend {
server localhost:3000;
}
server {
listen 80;
location / {
set $memcached_key $uri;
memcached_pass 127.0.0.1:11211;
error_page 404 = #fallback;
}
location #fallback {
proxy_pass http://backend;
}
}
}
It reverse proxy's the request when hitting port 80, but the logs always say:
2016/08/23 15:25:19 [info] 68964#0: *4 key: "/users/12" was not found by memcached while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /users/12 HTTP/1.1", upstream: "memcached://127.0.0.1:11211", host: "localhost"

Nginx Memcached module does not write to the Memcached server. You should do this in your backend (for example PHP) using the $memcached_key

Related

Open resty lua script in localhost:8082 not resolving http://localhost/foo/hostname

I am trying to access a localhost service (http://localhost/foo/hostname) using an open resty lua script using proxy_pass in the wsl-Ubuntu running in a windows 10 machine.
This is the content of nginx.conf
worker_processes 2;
events {
worker_connections 1024;
}
http {
server {
listen 8082;
location /{
resolver 8.8.4.4;
set $target 'localhost/foo/hostname';
proxy_pass http://$target;
}
}
}
I got a '502 Bad gateway' response when doing a curl to localhost:8082
This is the error found in the error.log file
2023/01/27 09:31:17 [error] 331#331: *1 localhost could not be resolved (110: Operation timed out), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8082"
I tried out various solution including modifying the resolver option.
This is issue was solved by specifying the localhost IP address (127.0.0.1) instead of localhost in the specific url.
worker_processes 2;
events {
worker_connections 1024;
}
http {
server {
listen 8082;
location /{
resolver 8.8.4.4;
set $target '127.0.0.1/foo/hostname';
proxy_pass http://$target;
}
}
}
If anyone know what the actual scenario or reason behind being unable to resolve 'localhost' but possible with '127.0.0.1', kindly share your knowledge.

Nginx auth_request handle saml 302 redirects

I am using Nginx as a reverse proxy. Also, I am using auth_request feature to protect my resources. For that, auth_request always goes through authrization server where I am using SAML authentication. While authenticating, there is a redirect 302 to IDP (ssocircle in this case) which I am trying to follow at Nginx but I am always ending up with below error:
no resolver defined to resolve idp.ssocircle.com while sending to
client, client: 127.0.0.1, server: , request: "GET / HTTP/1.1",
subrequest: "/saml/login", host: "localhost:9000"
*1269 auth request unexpected status: 502 while sending to client, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host:
"localhost:9000"
Here is my Nginx configuration:
server{
listen 9000 ;
auth_request /saml/login;
proxy_intercept_errors on;
error_page 301 302 307 = #handle_redirects ;
location /saml/login {
resolver 46.4.112.4 valid=300s;
internal;
proxy_set_header Host $host;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_pass http://localhost:9090/ ; # url to authorization server
}
location #handle_redirects {
set $saved_redirect_location '$upstream_http_location';
proxy_pass $saved_redirect_location;
}
location / {
root data/www;
}
location /images/ {
root data/ ;
}
}
I tried adding the resolver 46.4.112.4 IP of ssocircle but it didn't work.
Basically, I want to follow the redirect from my authorization server and have user to authenticate themselves at the IDP and return back to the original URL.
I am very new to Nginx. Any help will be much appreciated.
Edit : I am able to resolve the issue stated above. Restarting the Nginx worked for me. But again I am getting this error:
auth request unexpected status: 302 while sending to client,

Nginx forward proxy based on header value

I want to use nginx as forward proxy, but rewrite (also the host part) the URL based on a header value.
Suppose the browser connect to nginx on port 8888 with a regular http request. The header ha the pair:
X-myattribute: https://somehost.com
nginx should proxy_pass to https://somehost.com
My nginx.conf is now:
server {
listen 8888;
proxy_connect;
proxy_max_temp_file_size 0;
resolver 8.8.8.8;
location / {
proxy_pass https://$http_myattribute;
# proxy_pass http://$http_host$uri$is_args$args;
proxy_set_header Host $http_host;
}
}
}
but I get:
2018/08/16 19:44:08 [error] 9#0: *1 invalid port in upstream "https://somehost.com:443", client: 172.17.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8888"
2018/08/16 19:47:25 [error] 9#0: *1 invalid URL prefix in "https://somehost.com:443", client: 172.17.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8888"
(two lines depending if I set proxy_pass http://$X-myattribute or proxy_pass https://$X-myattribute or proxy_pass $X-myattribute. Assume X-myattribute always have http:// or https://)
Any suggestion?

django /admin not found

I try to setup Nginx+Gunicorn and when I go by my URL the Nginx redirects request to my app and handles it by itsels for static resource (static folder). Below my Nginx domain config:
server {
listen 80;
server_name asknow.local www.asknow.local;
root /home/ghostman/Projects/asknow/asknow;
location = /favicon.ico { access_log off; log_not_found off; }
location = /static/ {
root /home/ghostman/Projects/asknow/asknow;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/ghostman/Projects/asknow/asknow/asknow.sock;
}
}
Gunicorn daemon:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ghostman
Group=www-data
WorkingDirectory=/home/ghostman/Projects/asknow/asknow
ExecStart=/home/ghostman/Projects/asknow/env/bin/gunicorn --access-logfile /home/ghostman/Projects/asknow/env/log/gunicorn.log --error-logfile /home/ghostman/Projects/asknow/env/log/gunicorn-error.log --workers 3 --bind unix:/home/ghostman/Projects/asknow/asknow/asknow.sock asknow.wsgi:application
[Install]
WantedBy=multi-user.target
The problem that I need to Nginx handles request by itself for static only (www.asknow.local/static) but it tries to handle other URLs too. So when I go to www.asknow.local/admin now Nginx tries to find a resource by path (my_project/admin). But if I go to www.asknow.local Nginx proxies a request to the Gunicorn. Gunicorn error log is empty, so it fails on Nginx side.
Nginx log
2017/11/01 04:27:22 [error] 13451#13451: *1 open() "/usr/share/nginx/html/static/img/search.svg" failed (2: No such file or directory), client: 127.0.0.1, server: asknow.local, request: "GET /static/img/search.svg HTTP/1.1", host: "www.asknow.local"
How to fix it?
Your issue is using =, that is use for absolute locations. You don't want that (only for favicon.ico) you want that
server {
listen 80;
server_name asknow.local www.asknow.local;
root /home/ghostman/Projects/asknow/asknow;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ghostman/Projects/asknow/asknow;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ghostman/Projects/asknow/asknow/asknow.sock;
}
}

[Nginx][Gogs] Serving gogs through nginx

I'm running through an issue setting up Gogs through Nginx on my raspberry.
I just want to be able to redirect http://raspberry-ip-address:3000 to http://raspberry-ip-address/gogs.
Below my nginx virtualhost conf :
server {
listen 80;
server_name localhost;
location /gogs/ {
proxy_pass http://localhost:3000;
}
}
When I go on http:// raspberry-ip-address:3000, I get the installation page from gogs -> so Gogs is runnning well.
When I go on http:// raspberry-ip-address/gogs, I got a 404 Not found error. however the log from Gogs is somehow "reacting" because I get :
[Macaron] 2016-08-24 14:40:30: Started GET /gogs/ for 127.0.0.1
[Macaron] 2016-08-24 14:40:30: Completed /gogs/ 302 Found in 1.795306ms
2016/08/24 14:40:30 [D] Session ID: 8e0bbb6ab5478dde
2016/08/24 14:40:30 [D] CSRF Token: YfL58XxZUDgwim9qBCosC7EXIGM6MTQ3MTk4MDMxMzMxMTQ3MjgzOQ==
For more information here is my nginx/error.log :
request: "GET /localhost HTTP/1.1", host: "192.168.1.15"
2016/08/24 14:40:30 [error] 3191#0: *4 open() "/usr/share/nginx/html/install" failed (2: No such file or directory), client: 192.168.1.12, server: localhost, request: "GET /install HTTP/1.1", host: "192.168.1.15"
It seems to me that Nginx is not redirecting correctly the request. Any idea ?
Thanks ;)
For me the following config works:
location /gogs/ {
proxy_pass http://localhost:3000/;
}
but the following (what you posted) produces the error you mentioned:
location /gogs/ {
proxy_pass http://localhost:3000;
}
note the / and the and of the url.
A HTTP redirect (30x) does not solve the problem, because it will redirect to localhost which is not the raspberry pi but the computer that does the request.
Complete nginx conf in /etc/nginx/nginx.conf:
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /git/ {
proxy_pass http://127.0.0.1:3333/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}

Resources