Nginx - reverse proxy prevent needing port number in url - nginx

I have a React app that runs on https://localhost:3000.
I have a domain like https://example.com/some/path.
When I enter https://example.com/some/path into my browser, I'd like it to redirect to https://localhost:3000. So the way I've seen this possible is using a reverse proxy and I'm trying to nginx setup to do so.
However I keep facing in an issue that I need include the port number when I go to the full url for example: https://example.com:3000/some/path.
How do i prevent this from happening? i.e. I'd like to just enter https://example.com/some/path.
My nginx conf file looks like this:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 443 ssl;
server_name example.com;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
location /some/path {
return 301 $scheme://localhost:3000$request_uri;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 3000 ssl;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
server_name localhost;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
location / {
proxy_pass https://localhost:3000;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}
And in my hosts file I have the line:
127.0.0.1 example.com
Even if I keep it simple and have only one server section with location like:
location /some/path {
proxy_pass https://localhost:3000;
}
That kind of works - however looking at the console the assets themselves don't load https://example.com/static/js/bundle.js when it needs to be https://example.com:3000/static/js/bundle.js
Any help would be appreciated.

Related

How to correctly set a custom 403 ERROR page in Nginx

I can't seem to figure out how to setup a custom 403 error page in Nginx for IPs that are denied. No matter what I do, only the builtin Nginx "Forbidden" page shows up. Below is my nginx.conf file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
autoindex_localtime on;
include ./conf/blockips.conf;
server {
listen 80;
server_name localhost;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate ./ssl/fullchain.cer;
ssl_certificate_key ./ssl/cert.key;
server_name_in_redirect off;
log_not_found off;
server_name www.mywebsite.com mywebsite.com;
error_page 403 /error403.html;
location = /error403.html {
root html;
allow all;
}
location / {
root /inetpub/wwwroot;
index index.html index.htm;
}
}
}

Nginx: How to redirect each http:port requests to HTTPS:port in the below config?

Here is my nginx.conf, works fine for https.
If someone types HTTP://dev.local.org:3002, how do I redirect to HTTPS://dev.local.org:3002 ?
This nginx is inside a docker-compose container.
worker_processes 1;
events {
worker_connections 1024;
}
#set $my_server_name _ #TODO global variable does not work?
http {
#DOCKER DNS - using this to resolve docker-compose hosts like 'appsearch', 'kibana' etc
resolver 127.0.0.11 ipv6=off;
#include mime.types;
default_type application/octet-stream;
#TO read external configuration
include sites-enabled/*.conf;
server { #DEFAULT SERVER
listen 443 ssl; # Security change
server_name _;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
root html;
index index.html index.htm;
include common_location.conf;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# location /appsearch { #TODO this /appsearch did not forward. find how to do it.
# rewrite ^/appsearch(.*) /$1 break;
# resolver 127.0.0.11 valid=30s ;
# set $backend http://appsearch:3002;
# proxy_pass $backend; # Use variable To avoid upstream host not found error.
# }
}#server80
server {
listen 9200 ssl;
server_name _;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
set $backend http://elasticsearch:9200;
proxy_pass $backend; # Use variable To avoid upstream host not found error.
include common_location.conf;
}
}#server
server {
listen 3002 ssl;
#server_name dev.local.org; #TODO yuck, bad to add server name!
server_name _;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
set $backend http://appsearch:3002;
proxy_pass $backend; # Use variable To avoid upstream host not found error.
include common_location.conf;
}
}#server
server {
listen 5601;
server_name _;
include common.conf;
include /etc/nginx/ssl.conf;
location / {
set $backend http://kibana:5601;
proxy_pass $backend; # Use variable To avoid upstream host not found error.
include common_location.conf;
}
}#server
}
Use the 497 HTTP error to redirect: (source: https://meabed.com/http-497-status-code/)
In your conf you would add something like this:
listen 1234 ssl;
server_name your.site.tld;
ssl on;
error_page 497 https://$host:1234$request_uri;
}```

https to http for an old program

(sorry, bad english)
I have an old software, that call webpages.
But it cannot call https-pages, only http.
My nginx.conf for nginx:
worker_processes 1;
events {worker_connections 1024;}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#------------------
# from me:
server {
server_name localhost; listen 30001;
set $x https://www.example1.com/downloads;
location / { proxy_pass $x; }
}
server {
server_name localhost; listen 30002;
set $x https://www.example2.com/news;
location / { proxy_pass $x; }
}
...
#------------------
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {root html;}
}
}
Then in the software I can use:
http://localhost:30001 -> calls https://www.example1.com/downloads
http://localhost:30002 -> calls https://www.example2.com/news
Disadvantage:
I must write a server{}-Block for EVERY https-webpage into the nginx.conf.
Is ther a way to do https->http for ALL https-webpages?
Thank you very much for any help.

Nginx server not available to the public

I thought this Nginx setup was supposed to be easy :( I can get my index.html to load on localhost only but when I try to access the site by my domain name it doesn't work, or even if I try the server IP address externally it doesn't resolve :( What am I doing wrong? The is on Windows. Here is my conf;
#user nobody;
worker_processes auto;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 5;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
root C:/xampp/htdocs;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
the issue was that I didn't enable Nginx in the Windows Firewall. Working now.

hls is not allowed here

this is my configuration and I'm getting an error:
'hls' is not allowed here
http {
access_log logs/rtmp_access.log;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /hls {
hls;
hls_fragment 5s;
hls_buffers 10 10m;
hls_mp4_buffer_size 1m;
hls_mp4_max_buffer_size 5m;
root /run/shm;
}
#run/shm/hls/index.m3u8
# rtmp stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# you can move stat.xsl to a different location
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
so where do I suppose to put hls? I want to use http address and m3u8 file for show some video files on jwplayer
Are you using NGINX Plus or the third party module, nginx-rtmp-module, for this functionality? Note that HLS support is not otherwise supported.
If you're using the third-party module reference the nginx.conf example in the documentation.
(Disclaimer: I am affiliated with NGINX, Inc - the company that develops NGINX and NGINX Plus).

Resources