nginx reverse proxy to different port on another server running Apache/WordPress - wordpress

I am trying to setup nginx as a reverse proxy from one debian environment, to another debian environment. The two environments are on different IPs, and in different locations. Here is my configuration on the nginx reverse proxy.
server {
listen 80;
server_name foo.domain.com domain.com;
root /var/www;
if ($http_host != "foo.bar.com") {
rewrite ^ foo.bar.com$request_uri permanent;
}
index index.php index.html index.htm;
location / {
proxy_pass http://bar.domain.com:2000/;
proxy_redirect http://bar.domain.com http://foo.domain.com/;
}
}
Apache wordpress site is working on http://bar.domain.com:2000, however http://foo.domain.com does not transfer to it. When using foo.domain.com the address changes to foo.domain.com:2000 after it times out.

Related

How to create reverse proxy for multiple websites in nginx

I have many different technologies serving APIs and sites on my local machine. I want to be able to see them via human-readable names, rather than ports.
For example, I have:
localhost:8000 => laravel api for user panel
localhost:8001 => laravel api for admin panel
localhost:3000 => react client for user panel
localhost:3001 => nextjs client for site
localhost:3002 => react client for admin panel
And this list goes on.
Remembering all these ports is not possible of course. Thus I thought to setup a reverse proxy for them:
api.user.example.local
api.admin.example.local
example.local
user.example.local
admin.example.local
I know I have to add these host headers to /etc/hosts file. I also read about how to configure nginx as a reverse proxy for one domain.
I don't know how to do it for many sites. And only as a reverse proxy, not as a server.
Please note: I'm not considering myself as really super nginx expert, just starting to learn nginx, but I think I can help you with this task.
Here is my approach:
First, make sure your default nginx config (usually /etc/nginx/nginx.conf) has line include /etc/nginx/conf.d/*.conf; in its http block, so you may specify internal servers in separate config files for ease of use.
Create additional config file /etc/nginx/conf.d/local_domains.conf and add following server blocks in it:
server {
listen 80;
server_name api.user.example.local;
location / {
set $target http://localhost:8000;
proxy_pass $target;
}
}
server {
listen 80;
server_name api.admin.example.local;
location / {
set $target http://localhost:8001;
proxy_pass $target;
}
}
server {
listen 80;
server_name example.local;
location / {
set $target http://localhost:3000;
proxy_pass $target;
}
}
server {
listen 80;
server_name user.example.local;
location / {
set $target http://localhost:3001;
proxy_pass $target;
}
}
server {
listen 80;
server_name admin.example.local;
location / {
set $target http://localhost:3002;
proxy_pass $target;
}
}
On the client machine, add these records to the hosts file
192.168.1.1 api.user.example.local
192.168.1.1 api.admin.example.local
192.168.1.1 example.local
192.168.1.1 user.example.local
192.168.1.1 admin.example.local
Where 192.168.1.1 is the address of your nginx server.
That's it, it should work if your internal servers are using HTTP protocol.
But if you need to use HTTPS for internal servers and for the main nginx server, modify each server block as follows:
server {
listen 443 ssl http2;
server_name api.user.example.local;
ssl_certificate /usr/local/share/ca-certificates/example.local.crt;
ssl_certificate_key /usr/local/share/ca-certificates/example.local.key;
add_header Strict-Transport-Security "max-age=31536000" always;
location / {
set $target https://api.user.example.local:8000;
proxy_pass $target;
}
}
and so on
ssl_certificate and ssl_certificate_key should point to correct certificate and key files for the domain.
If you would like nginx main server to listen port 80 and redirect all traffic to https, add additional server blocks for each server:
server {
server_name api.user.example.local;
listen 80;
# Force redirection to https on nginx side
location / {
return 301 https://$host$request_uri;
}
}
and so on
More information on NGINX Reverse Proxy
NGINX Reverse Proxy
Module ngx_http_proxy_module

nginx Reverse Proxy with 2 servers

So i've came across a cool project and i wanted to recreate it. It is my first time using nginx and also my first time learning things about a reverse proxy. I've currently have a reverse proxy running and it works (I guess). But the Proxy currently only works with other ports. I have 3 servers that are running nginx. I use one of them as my reverse proxy. I can access the other servers with different ports. See here (reverse-proxy.conf):
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.20;
}
}
server {
listen 8080;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.30;
}
}
Are there a way to use the reverse proxy without using different ports? Or is my solution ok? At the end i just need a reverse proxy that is able to communicate with 2 other servers.
So one thing here people use reverse proxy in a different ways
But most generic usecase is redirect using location.
Please find the below example.
server {
listen 80;
root /var/www/html;
server_name localhost;
location /a {
proxy_pass http://192.168.2.20;
}
location /b {
proxy_pass http://192.168.3.20;
}
}
Another is giving weight to each proxy.
Please find the below example
stream {
upstream stream_backend {
server http://192.168.2.20 weight=75;
server http://192.168.3.20 weight=25;
}
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass stream_backend;
}
}
In above 192.168.2.20 will receive 75% of the load and 192.168.3.20 will receive 25% of the load. In case if you want to distribute the equal load to both(or round-robin method) Please remove the weight.
I think you may not understand how Nginx work about proxy.
Nginx can reverse Proxy L7 http or L4 stream
and you set the proxy listen on any port or URL you want and proxy to any server or port or URL you want.
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.20:2323/URL;
}
}
server {
listen 8080;
root /var/www/html;
server_name localhost;
location / {
proxy_pass unix:/tmp/backend.socket;
}
}
Here is a reference for you about the proxy_pass directive.
proxy_pass

simple Nginx configuration on t2 (AWS EC2) server gives 504 Gateway Time-out on subdirectories

I have a t2 server (AWS EC2 server) that I want to simply serve an HTML file as the root:
http://my_ip/ -> gives me my index.html
and another one serving a reverse_proxy
http://my_ip/api -> gives me my "API" (or whatever runs on the reverse proxy IP)
So I configured the Nginx as the followings:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_pass http://127.0.0.1:3000;
}
}
Simple enough, in this configuration, my index.html is showing up. But whenever I'm trying to access the "reverse proxy" (/api), I get 504 Gateway Time-out error.
So I tried to "switch places", I gave the reverse-proxy the "root" / and the index.html the "/api".
Getting this configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://127.0.0.1:3000;
}
location /api {
try_files $uri $uri/ =404;
}
}
This way, the reverse proxy is doing OK! (from /), and when I try to reach index.html (from /api), I'm getting 504 Gateway Time-out error.
So I figured that something is wrong with the "sub-directories" configuration. I tried such configurations on many servers before (mainly from DigitalOcean and others) and never ran into something like that.
My guess is it got something to do with the t2 (EC2) NAT configuration (maybe?). But I'm not so sure.
Googling the issue didn't raise any answers.
There's Ubunutu 20.04 installed on the t2 server, It's "brand new" (nothing was installed but Nginx, Node & NPM - was done by me, "by the book" installations with no needed configurations or file changing).
Note that I don't have a domain yet, I was thinking of trying to server the reverse proxy on a subdomain but I have no way to do it at this point, that's why I tried to do it via a subdirectory.
Thanks.

Nginx proxy_pass rule Issue

So, I'm running some docker containers serving on ports 8090 and 8000. Now I want to setup Nginx reverse proxy to handle requests to both of these ports internally. The main URL http://milesblock.com changes automatically to http://milesblock.com/#/
I have setup a proxy_pass in nginx as follows-
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name milesblock.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:8090;
}
location /api {
rewrite ^/api(.*) $1 break;
proxy_pass http://127.0.0.1:8000;
}
}
Now, the issue is because of the automatic change of the URL to http://milesblock.com/#/ the redirect to both the ports is not working as intended. Only the /api proxy is working with the above config file.
How do i configure the proxy to handle the traffic on port 8090 and also the api calls on port 8000?

nginx proxy_pass missing path

So I am running some nginx and jenkins in docker containers (same machine).
I have setup a proxy_pass in nginx as follows
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /jenkins/ {
proxy_pass http://infrastructure_jenkins_1:8080/;
}
}
The redirect goes to http://54.194.42.13/static/b703e301/css/layout-common.css which results in a 404.
How do i configure the proxy to include /jenkins in the path i.e. http://54.194.42.13/jenkins/static/b703e301/css/layout-common.css ?
Remove the trailing / from the proxy_pass statement. The trailing / is instructing nginx to substitute the value of the location statement (/jenkins/) with /. See this document for more.
For example, to reverse proxy without altering the original URI use:
location /jenkins/ {
proxy_pass http://infrastructure_jenkins_1:8080;
}

Resources