Nginx Reverse Proxy - nginx

I have an Nginx instance set up with a reverse proxy like so:
server {
listen 80;
server_name sitep-rate00.sv.walmartlabs.com;
location / {
proxy_pass http://www.example.com;
}
}
How can I set the css style sheet to a specific version, for example something like:
Substitute 's|styles/sheets.1.1.1(.*)\.css"|styles/sheets.1.1.33.css"|i'

If you are planning to proxy pass it too you can try a rewrite before the proxy pass
location / {
rewrite /styles/sheets.1.1.1(.*)\.css /styles/sheets.1.1.33.css last;
proxy_pass example.com;
}

Related

nginx proxy_pass removing base path

So I currently have a site at http://www.example.com and I would like that all requests from
http://www.example.com/api/v2/<anything>
to be proxied to
http://api.v2.com/<anything>
Note that http://api.v2.com does not have a base path.
My config:
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
This way, however, the requests are being proxies to http://api.v2.com/api/v2/<anything> keeping the original path, and not http://api.v2.com/<anything>.
I've already noticed that:
location /api/v2 {
proxy_pass http://api.v2.com/api;
works as desired - http://api.v2.com/api/v2/<anything> proxies to http://api.v2.com/api/<anything>.
Is there a way to achieve the first behavior (that is , http://api.v2.com/api/v2/<anything> to http://api.v2.com/<anything>)? Do I have to use a rewrite? I know it it related to normalized URIs and that is expected, just would like to know if there's a way to do it.
If you want to remove the proxy path from resulting url to the server, either you should have the uri part in the proxy url, or you should specify the rewrite part.
In this specific case, it should be like as follows,
location /api/v2/ {
rewrite /api/v2/(.*) /$1 break;
proxy_pass http://api.v2.com/;
}
for more information visit nginx documentation https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com/;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Nginx dynamic proxy configuration

Below is a static config of what I'm trying to do.
server {
listen 80;
server_name browser.shows.this.server.com;
location / {
proxy_set_header Host backend.server.com;
proxy_redirect http://backend.server.com/ http://browser.shows.this.server.com/;
}
}
How can I make backend.server.com dynamic for each request? I'd like to pass the domain somehow in the request. Maybe in a header?
You should use proxy_pass instead of proxy redirect. Hope this helps
alternatively can write a config like this
resolver your-server-ip;
set $upstream_endpoint http://your-url;
location / {
rewrite ^/(.*) /$1 break;
proxy_pass $upstream_endpoint;
}
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Modify $request_uri in Nginx

I have multiple apps running on the Nginx server:
http://example.com/app1/ctrl/view
http://example.com/app2/ctrl/view
...
I would like to assign these apps DNS like so:
http://app1.example.com
http://app2.example.com
...
For that I've tried the following server block:
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://example.com/app1/$request_uri;
}
}
If a user is not logged in, my app would redirect to URI:
app1/ctrl/user/login?_next=/app/ctrl/view
Essentially $request_uri becomes (Note doubled app1 instance):
app1/app1/ctrl/user/login?_next=/app/ctrl/view
Is there a convenient way to modify $request_uri or a better method to get around this problem?
EDIT1
It seems I've solved my problem with the following server block:
server {
listen 80;
server_name app1.example.com;
location / {
set $new_request_uri $request_uri;
if ($request_uri ~ ^/app1/(.+)$) {
set $new_request_uri $1;
}
proxy_pass http://example.com/app1/$new_request_uri;
}
}
If someone knows a better (or proper "Nginx") way to do this please don't hesitate to post an answer.
EDIT2
Based on the comments I've also tried the following:
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://example.com/app1/;
proxy_redirect /app1/ /;
}
location ~ ^/app1/(.+)$ {
return 301 http://$server_name/$1;
}
}
This one looks better on screen, as it eliminates app1 instance in the $request_uri part completely, but you must have two location blocks.
EDIT3
The most efficient way to solve my problem apparently is as shown in this config:
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://example.com/app1/;
proxy_redirect /app1/ /;
}
location /app1/ {
rewrite ^/app1(.+) $1 permanent;
}
}
This is due to the fact, that Nginx always tries to match the longest prefix first and then (if ^~ modifier is not present) starts sequentially processing regexes until the first regex match is found. Essentially this means that all regexes are processed on every request, regardless if any of these find a match, therefore it's better to have regexes inside location directives.
You don't need to go complex way. Solution is much simpler
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://app1.example.com/app1/;
}
location /app1/ {
proxy_pass http://app1.example.com/app1/;
# or
# rewrite ^/app1(.+) $1 permanent;
}
}
Nginx will take care of adding /app1/ to request and strip it from Location header.
See proxy_redirect directive.

How can i pass subdomain as proxy_pass value in nginx?

i am currently in situation where i need to get/catch sub-domain and pass that sub-domain value to proxy_pass in Nginx config.
e.g.
if user enters
http://google.com.mydomain.com
then it should do proxy pass as
proxy_pass http://www.google.com/;
in above example google.com is sub-domain
can this be doable ?
How can i achieve something like that in nginx ?
currently i am using configuration where sub-domain values are hard-coded in config files , but there are many sub-domains , so i need to do it like this way, but don't know correct syntax.
server {
listen 80;
server_name subdomain.domain.com;
charset utf-8;
location / {
proxy_pass http://www.subdomain/;
}
}
I am using * as A record to redirect all sub-domains to my web-host, i.e. wildcard DNS.
update:
i have found code snippet from https://stackoverflow.com/a/22774520/1642018
server {
listen 80;
# this matches every subdomain of domain.
server_name .domain.com;
location / {
set $subdomain "";
if ($host ~* "^(.+)\.domain.com$") {
set $subdomain $1;
}
proxy_pass http://$subdomain;
}
}
but the request is showing my default page which is in my default web root.
Two things.
1- A resolver (a dns server for your nginx in order to resolve google.com, you may add at your hosts or you are able to add resolver statement)
2- You will need to resolve how your client will deals with different domains, I mean google.com is different than google.com.ar or google.fr)
At this example I made work it for your example google.com
worker_processes 4;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
set $subdomain "";
if ($host ~* "^(.+)\.domain.com$") {
set $subdomain $1;
}
resolver 8.8.8.8;
proxy_pass "http://$subdomain.com";
}
}
}
I hope that this configuration helps to you.
I would capture the subdomain using a map then proxy pass if the variable is defined:
map $host $subdomain {
~^(?<sub>.+)\.[^\.]+\.[^\.]+$ $sub;
}
server {
listen 80 default_server;
server_name _;
location / {
if ($subdomain) {
proxy_pass http://$subdomain;
}
}
}

How to rewrite localhost:9292/json to localhost:80/ using Nginx reverse proxy?

server {
listen 80;
server_name localhost;
location / {
index index.html;
root /Users/Lin/Codes/JS/Emberjs/yeoman-ember/dist;
}
location ~* ^/json {
root
proxy_pass http://localhost:9292;
}
}
The configure kinda works, but it only pass
localhost:9292/json to localhost/json.
But What I want is
localhost:9292/json to 'localhost'
`localhost:9292/json/post to 'localhost/post'
I think what I need to do is set root or do some rewrite, Anyone has some idea?
add a rewrite rule before the proxy_pass
location /json {
rewrite ^/json(.*) $1;
proxy_pass http://localhost:9292;
}

Resources