Nginx proxy remove spesific path and emty Post request body +HTTPS - nginx

I'm using nginx for web service proxy. I have rest service as below and i want to proxy my domain
https://www.example.com/myRestservice. Service has some method like this;
http://1.1.1.1:123/api/work/method1
http://1.1.1.1:123/api/work/method2
*By the way services publish on two server as below in nginx.conf
As result i want to access to methods of service like "https://www.example.com/Restservice/api/work/method1"..
When i try to use rewrite in nginx as below, i can access service.
But in this time Post method's request body is emty. I can see service logs.
In my nginx.config
upstream RestService {
server 1.1.1.1:123;
server 1.1.1.2:123;
}
server {
listen 443 ssl;
server name https://www.example.com;
location ~ ^/Restservice/ {
add_header Access-Control-Allow-Origin *;
rewrite ^/Restservice/(.*) /$1 break;
proxy_pass http://Restservice/;
proxy_http_version 1.1;
}
}
Bye the way i try to location part like this, result is same.
location /Restservice {
proxy_pass http://Restservice/;
}
Normally I can access soap service with config from https link.
Is it about http redirection to https ?
In nginx access log;
status : 500
request: POST /Restservice/api/work/method1 HTTP/1.1

I find the reason. Because of the endcoding.
After choosing endcoding type 'UTF-8', I could see request body.

Related

nginx proxy_pass with upstream not working and gives a 404 error

I have set up IIS with NGINX in the following manner:
public_facing_IIS --> NGINX[rate limiting] --> internal_IIS
Users hit the public website url which forwards the request to NGINX which then forwards it to an internal IIS server. The reason for this chain is that I need to limit hits from NGINX to internal_IIS to a particular number-I don't want rate limiting at the public url end. In doing so when setting up an upstream with proxy pass I get a 404 error.
So this works for any link on the public facing IIS web site, but once I get into the path /Billing/ I get a 404 error
http{
upstream myupstream {
server internal.IISwebsite:80;
}
server {
listen 81;
server_name internal.nginx;
location / {
proxy_pass http://internal.IISwebsite;
}
location /Billing/ {
limit_req zone=mylimit burst=20;
proxy_set_header Host $host;
proxy_pass http://myupstream/;
}
}
}
I tried this even with by-passing the external_IIS url and calling the internal.nginx:81 directly. It all works well for every other url on http://internal.nginx:81. The minute I hit http://internal.nginx:81/Billing/ is when I get a http 404 error.

nginx - Passing request header variables to upstream URL as query parameter

I have an application running on localhost listening on port 8080
nginx is running as reverse proxy, listening on port 80
So, a request coming to nginx on port 80 is sent to this application listening on localhost:8080 and response from this application sent back to the user
Now this application is incapable of reading the header variables from request header and can read only query parameters
So I want nginx to pass header values as query parameters to this application listening on localhost:8080
E.g. let us say in the request header there is a custom variable called 'userid'.
How do we pass this userid as &userid=value appended to the url to application listening on localhost 8080
My current test file of site-available and site-enabled is
server {
location /test {
proxy_pass http://localhost:8080;
}
}
So there is no need to do rewrite or anything else. Simply pass the header parameters that you want to pass as query parameter to the localhost application like below by appending to the arguments.
If you have custom header parameter like userid, then it would be $http_userid
server {
location /test {
set $args $args&host=$http_host;
proxy_pass http://localhost:8080;
}
}
If you have a request header called userid, it will be available within an Nginx variable called $http_userid.
You can alter the query parameters of the original request with a rewrite...break statement.
For example:
location /test {
rewrite ^(.*)$ $1?userid=$http_userid break;
proxy_pass http://localhost:8080;
}
See this document for details.

How to proxy_pass x-access-token (JWT) header in nginx?

I am trying to use nginx as a reverse proxy, but I couldn't find anything on how to forward the x-access-token (JWT) header.
The reverse proxy works fine, and using http://localhost/app gives me the api. However, when I try to access anything requiring a token, ex. http://localhost/app/api/products I get Cannot GET //api/products.
In the documentation, it says that the default is proxy_pass_request_headers on; but the problem doesn't seem to be there.
nginx.conf
events {}
http {
server {
listen 8000;
location /app {
proxy_pass 'http://localhost:3000/';
}
}
}

How to rewrite URL to match a server using nginx?

I'm new to nginx. I have two projects, and the one is django web app which is running localhost 8000, and another is tornado which used to provide api service and running localhost 8888.
How do I config the nginx that redirects all the url requests(from 80 port) to localhost:8000 but /api requests to localhost:8888(tornado app)?
Edit your nginx config file. Add a server block and use proxy_pass in location blocks to proxy (redirect) the request.
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8000;
}
location /api {
proxy_pass http://127.0.0.1:8888;
}
}
Save it, and reload nginx.
nginx -s reload
https://gist.github.com/soheilhy/8b94347ff8336d971ad0

is there a way to fallback nginx proxy_pass if header set

nginx server serves http://server1.com, http://server2.com and http://server3.com.
nginx upstreams request process to some ruby code.
server1.com, server2.com and server3.com are actually some static files stored on amazon s3.
I want to do next: find bucket name for 'server1' host, put in db some logs and notify nginx to stream from amazon.
Maybe via setting in ruby code header with url to amazon s3 bucket and using this url later by nginx.
The flow: browser -> nginx -> ruby -> nginx -> amazon_s3 -> browser
I found how i can do this on error:
http {
server {
listen 12345; #Port that my custom app was assigned
server_name mydomain.com;
location / {
proxy_intercept_errors on;
error_page 400 403 502 503 504 = #fallback;
proxy_pass http://the_old_site_domain.com;
}
location #fallback {
proxy_pass http://myfallback.domain.com;
}
}
}
But is there a way to do something similar based on header appereance?
Thanks!
UPD
This is how i can test my header:
if ($http_x_custom_header) {
....
}
If set nginx should do some internal redirect, right?
But how it can be invoked after ruby code?
There is special headers called X-Accel-....
You need X-Accel-Redirect.

Resources