Redirect whatever.net to whatever.com without explicitly writing "whatever" in nginx.conf - nginx

I have a web app, that will be running on many different domains (and possibly subdomains). Each domain/subdomain will be available as both .net and .com. I want to redirect every request of .net to .com.
Example:
www.whatever.net -> www.whatever.com
www.sub.whatever.net -> www.sub.whatever.com
whatever.net -> whatever.com
sub.whatever.net -> sub.whatever.com
somethingelse.net -> somethingelse.com
...
For various reasons I'd like to have only one nginx.conf file that works for every installation, so I can't write something like:
server {
server_name .net;
return 301 $scheme://whatever.com$request_uri;
}
Because this works just for the installation that's under the whatever.net/whatever.com domains. So I tried:
server {
server_name "~^(?<name>.+)\.net$";
return 301 $scheme://$name.com$request_uri;
}
But this does not work, it capture every request, not only those coming from the .net domain, and (on chrome at least) the result is that the content of the address bar become: .com/thequerypart.
I'm new to nginx, what am I doing wrong?
EDIT:
The rest of nginx.conf is another server block that starts with:
server {
server_name .com;
...
}
It works as intended without the other one.

I... didn't know browsers cache things that are "Moved permanently" (makes sense), the setup I've shown in the question worked, but a previous different attempt did not, and got cached. So, for anyone that may get here in the future, the answer is:
server {
server_name "~^(?<name>.+)\.net$";
return 301 $scheme://$name.com$request_uri;
}

Related

Nginx routing with hashtag and to different pages

I need to update my nginx .conf file for a site that will be moved on another domain and to another platform.
My conf looks like this:
server
{
listen 443;
server_name site1.domain.com;
return 301 https://site2.com$request_uri;
}
plus certificates etc.
The specific question is that i need to forward/rewrite 4 specific page, with the hashtag, to 4 different links.
site1.domain.com/request/#/ -> site2.com
site1.domain.com/request/#/ordering -> site2.com/ordering
site1.domain.com/request/#/products -> site2.com/products
site1.domain.com/request/#/distribution -> site2.com/distribution
What's the best method to achieve this?
Thanks,
Mvrck
i've tried different regex, but i'm not so skilled in that.

Nginx possibly removing dot (".") from URL path before forwardslash

I have defined a reverse proxy like this:
server {
listen 443 ssl;
server_name testing.com;
ssl_certificate "C:/nginx/testing.crt";
ssl_certificate_key "C:/nginx/testing.key";
location / {
proxy_pass "http://127.0.0.1:8888/";
}
}
The reverse proxy works as intended. Now that we got that out of the way:
I have a case where i need to pass parameters in the URL and some of the parameters sometimes end with a dot (.) like this "https://testing.com/param1./param2/param3/param4."
But for some reason the URL that is received at the server looks like this "127.0.0.1:8888/param1/param2/param3/param4"
If i call the server directly like this "127.0.0.1:8888/param1./param2/param3/param4.", the parameters are correct. My guess is that nginx modifies the URL. Maybe the issues lies somewhere else...
I am on Windows 10. The server is a Go (golang) server that uses only built in libraries. I have setup self signed certificates and edited my hosts file (never had issues with those).
ALSO - my friend who is also working on this project has no issues even tho we have identical nginx setups, but the only difference is that he is on Linux.

How to rewrite URL in NGINX with location parameter?

i have a Application (Openproject) on a Webserver.
this is Reachable under http://10.0.0.1:8000/
Behind my users and the Webserver is a NGinx on which i need to publish under a specific URL: https://ngrp.com/openproject
so i made the following changes in my Nginx Configuaion (in this NGINX instance multiple Websites are published with the "location" settings):
location /openproject/ {
proxy_pass http://10.0.0.1:8000/;
include /etc/nginx/conf.d/proxy.conf;
}
But when i open the page through the Reverseproxy, the Webbrowser displays only a White Page.
In the Webbrowser Debugger i see, that some paths are wrong, so the browser couldnĀ“t load it. Example:
https://ngrp.com/assets/frontend/styles.c3a5e7705d6c5db9cfc1.css
(/openproject/ is missing in the URL)
Correct would be:
https://ngrp.com/openproject/assets/frontend/styles.c3a5e7705d6c5db9cfc1.css
So can somebody please tell me, which configuration is needed, so i can Openproject under the URL https://ngrp.com/openproject/ successfully?
Thank you very much.
When you proxy_pass you proxy the entire HTTP request meaning that you are actually requesting GET /openproject on http://10.0.0.1:8000/ rather than just GET /
You can add this line before the proxy_pass to fix this and remove the /openproject prefix :
rewrite /openproject/(.*) /$1 break;
This changes the requested URL from /openproject/... to /...

Simple nginx config not working on CentOS

I have installed Nginx 1.12.2 on CentOS 7. I have an extremely simple nginx config and it is not working at all. I have setup several nginx instances on Ubuntu in the past without any issue I wonder if there is something to do with CentOS.
I have double-checked that the "root" directory exists and the files also exist with proper permissions. But I am getting 404 error. Also for debugging purpose, I tried to put "return 200 $uri" in the location block and it seems to be returning me the proper URI but try_files doesn't work
/var/www/mydomain/public/test.html exists with proper permissions
For debugging when I put "return 200 $uri" it shows up when I hit the domain
Hitting mydomain.com/test.html gives 404
server {
listen 80;
root /var/www/mydomain/public;
index index.html index.htm;
server_name mydomain.com;
location / {
# return 200 "$uri";
try_files $uri $uri/;
}
}
Few things:
Check your NGINX error log at /var/log/nginx/error.log, you will likely see what file is being accessed and make conclusions from that
Be aware of the presence of /etc/nginx/conf.d/default.conf, which is shipped with the package. It has default server, which is what NGINX will use when no domain has matched, however it's a sample file rather than a real config. I tend to just echo > /etc/nginx/conf.d/default.conf, to "remove it" in a safe way. (If you just remove the file, then package update will restore it, but if you nullify it like I do, then package upgrades won't touch it).

Why does nginx return a Error 324 with certain query strings?

I have two servers, with identical minimal configuration (as far as I know!).
On server A, a query for
http://xxx.yyy.zzz.A/
returns the default nginx index.html page
On server B, a query for
http://xxx.yyy.zzz.B/
returns the default nginx index.html page
On server A, a query for
http://xxx.yyy.zzz.A/?%2F
returns the default nginx index.html page
On server B, a query for
http://xxx.yyy.zzz.B/?%2F
returns Error 324 (net::ERR_EMPTY_RESPONSE)
%2F is a CGI encoded forward slash, which is how I found this problem. It also seems to happen on %2G, %2H and %2I. I stopped testing here.
The 324 request does not show in access or error logs.
The relevant nginx.conf is
server {
listen 80 default_server;
server_name "";
location / {
root html;
index index.html index.htm;
}
}
What could possibly be the issue, or how could I further track it down?
Often problems like "hey this works half the time" or "this works on server A but not on server B" are loadbalancing/ proxy problems.
Did you check the configuration of your loadbalancer? Big chance something is wrong there and the errors are logged there.

Resources