I have a problem to understand how nginx proxy_pass will handle the redirection after the URL getting rewrite.
What I have is localhost:8080/foo/log will automatically redirect to localhost:8080/foo/login. Once you enter the right credentials it will redirect back to localhost:8080/foo/log
localhost:8080/foo/log -----> localhost:8080/foo/login
I have a nginx proxy server to change my URL from localhost:8080/foo/log to localhost/web/foo/log with following configuration
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/html;
index index.html;
location /web/foo {
proxy_pass http://localhost:8080/;
rewrite ^/web/foo(.*) /foo$1 break;
}
location /web/bar {
proxy_pass http://localhost:8081/;
rewrite ^/web/bar(.*) /bar$1 break;
}
}
With this proxy config, I am ok to seelocalhost/web/foo/log proxy from localhost:8080/foo/log without enabling login redirection. If I enabled login, I always get localhost/web/foofoo/login.
I am wondering how can I get localhost/web/foo/login proxy from localhost:8080/foo/login ?
Related
I have the following on my NGINX server...
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/src/app/host.crt;
ssl_certificate_key /usr/src/app/host.key;
...
location /core {
proxy_pass http://auth-example-application-core-1:3000;
}
}
But when I try to access https://localhost/core I get a 404. If I open a shell and access the NGINX container itself I can hit the endpoint and get a response....
curl http://auth-example-application-core-1:3000
{"status":200,"message":"...","hostname":"5f5444c40d5b"}#
So why is it throwing a 404?
I Also tried...
location /core {
rewrite /core/(.*) /$1 break;
proxy_pass http://auth-example-application-core-1:3000;
}
That didn't seem to work either
How can I make nginx redirect all the requests to my subdomain to a folder?
Example:
http://sub2.sub1.domain.com/
that should indicate that sub2 is a folder in sub1.domain.com/sub2
How can I do this?
The main objective is to hide the folder to the user. So, it should continue as
http://sub2.sub1.domain.com/
My wish is to use a wildcard in sub2.
UPDATE:
I've tried:
server {
listen 80;
listen [::]:80;
server_name ~^(.*)\.sis\..*$;
location / {
proxy_pass http://sis.mydomain.com/$1$request_uri;
}
}
but it also didn't work, any error?
In the nginx directives for sub2.sub1.domain.com you'd put:
server {
listen 80;
server_name sub2.sub1.domain.com;
location / {
proxy_pass https://sub1.domain.com/sub2;
}
}
So any request going to sub2.sub1.domain.com gets proxied to → sub1.domain.com/sub2 (while masked as sub2.sub1.domain.com); no need for a redirect or rewrite this way either.
Wildcard Method
server {
listen 80;
server_name ~^(.*)\.sub1\.domain\.com;
location / {
proxy_pass https://sub1.domain.com/$1;
}
}
*the wildcard method above is untested.
I have my flask application deployed on Nginx over my VM.
Everything is deployed Ok and I can request my apis on http://my.ip.number (I have a public IP)
But when I run Ngrok (I need https and I don't have a domain name to generate a SSL certificate), the URL https//number.ngrok.io shows me the Nginx home page (Welcome to Nginx) instead my webapp.
Why is this happening?
P.D: When I run "curl localhost" I get the Nginx Welcome Page but when I exec "curl -4 localhost" I get my webapp home page
etc/nginx/site-available/myproject
server {
listen 80;
server_name 0.0.0.0;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name public.ip;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Any request coming in from ngrok, has the Host header set to the ngrok URL. The behaviour of nginx would be to try and match one of the server blocks in your configuration above, and default to the first one if no server_name matches the Host header.
However, I'm guessing there's another configuration file at /etc/nginx/conf.d/default.conf or /etc/nginx/sites-enabled/0-default which has a listen directive with default_server set. That will be catching these requests and serving the "Welcome to nginx!" page.
I suggest you look for that file, and remove it which should solve the issue.
However you could also simplify the above configuration and simply have:
server {
listen 80;
server_name localhost;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Provided there's not another server block hiding somewhere else in the configuration with a directive like listen 80 default_server; then this should catch all requests.
For more info see: How nginx processes a request
I have a 2 servers :-
Server 1 : NGINX Reverse Proxy.
Server 2 : NGINX with 5-6 websites ( different domains )
So basically, all users will come to Server 1 which will proxy_pass the traffic to Server 2 and get the response back. Server 1 will also do Caching, WAF etc.
Here is my configuration for Server 1 :-
server {
listen 80;
server_name example.com www.example.com;
location ~* {
proxy_pass http://mysite:80;
}
}
server {
listen 80;
server_name server.com www.server.com;
location ~* {
proxy_pass http://mysite:80;
}
}
In my Server 2, in virtual.conf of NGINX, i have the following config :
index index.php index.html;
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/websites/example/;
include location-php;
}
}
server {
listen 80;
server_name server.com www.server.com;
location / {
root /var/www/websites/server/;
include location-php;
}
}
However whenever i go to http://example.com or http://server.com ( directed via Sever 1 acting as Reverse Proxy ), it shows the Server 2's Default NGINX Page. I am not sure what am I doing wrong. Also is this type of setup a proper way of doing things ?
This is your host problem.
Due to your upstream name is mysite, so the host name in upstream request is mysqsite too.
So the host isn't matched by the backend servers.
You can solve the problem like this by adding the directive before proxy_pass:
proxy_set_header Host server.com
I use Nginx as a web server and I want to deny access to a particular directory from certain domains. Or in other words, make that path/directory only accessible from one domain or IP address.
Example:
http://domain.com/manager/ => Redirects to 404 page
http://www.domain.com/manager/ => Redirects to 404 page
http://10.10.10.10/manager/ => allows access
Finally, to make this a complete solution, I'd like to force an https connection when accessing to this particular path.
Following my previous example:
http://10.10.10.10/manager/ => Rewrites to https
Using
location / {
deny all;
allow [ip];
}
And restart your services.
More : http://nginx.org/en/docs/http/ngx_http_access_module.html
To address the force https (Assuming you already have a nginx that listens to port 443)
server {
listen 80;
server_name 10.10.10.10;
rewrite ^(.*) https://10.10.10.10$1 permanent;
}
To address the redirect to 404, you can consider using the following in your main config that listens for server "domain.com"
location ~ ^/manager {
return 404;
}
This is what I was looking for:
server {
listen 80;
server_name domain.com;
# Redirect path to secure connection
location ^~ /manager {
rewrite ^/(.*) https://domain.com$1 permanent;
}
}
server {
listen 443;
location ^~ /manager {
allow 10.10.10.10;
deny all;
error_page 403 =404 /404.html;
}
}