How to configure Nginx for URL mapping of CNAME requests? - nginx

I'm trying to configure my Nginx server block to accept requests without changing the requested domain, so that my users can use their custom domain with my application via a DNS CNAME-type entry.
The expected behaviour is as follows:
User creates a dns cname entry pointing to my server/domain e.g. www.userdomain.com to www.mydomain.com
My server then directs the request to my application www.mydomain.com/app while maintaining the URL www.userdomain.com
I've been able to make this behaviour work as above by setting my www.mydomain.com server block as default_server, but i'm trying to find a different way because i want to replicate this behaviour with a different server block/domain in the same server.
My current Server Block looks like the example below:
server {
listen 80 default_server;
root /var/www/mydomain.com/app;
index index.php;
server_name www.mydomain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
location ~ /(.*)$ {
try_files $uri $uri/ /;
}
}
Is it possible to reproduce this behaviour without setting the default_server flag?

Related

Configuring NGINX location to include subpaths

I have an NGINX location block configured as below. This redirects to an angular Application and works fine. However, when I navigate to a subpath in the angular application such as /path/subdir, then NGINX returns 404.
Apparently this location block only sends requests to /path but not /path/other to Angular.
location /path {
try_files $uri $uri/ =404;
}
I've tried variations such as these with the same result
location /path/
location /path/.*
How do I get NGINX so send all traffic sent to anything under /path to the same Angular application, so that the Angular application can then handle routing to sub-directories such as/path/subdir?
Well I ended up with this little horror show, but it seems to work.
location ~ ^/path(?:/(.*))?$ {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /path/index.html =404;
}

Reverse proxy nginx to itself

I am currently hosting a single-page react app that is hosted in the URL root like so:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I need to put the site behind an AWS elastic load balancer and at the same time change the path so everything is within a /support directory e.g. http://example.com/index.html -> http://example.com/support/index.html.
AWS ALBs do not support URL rewriting so I have to do this within the nginx config on the server. First of all I tried changing the config to:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
This sort-of works but the URLs within the javascript content don't contain the /support path (e.g. they contain http://example.com/script.js instead of http://example.com/support/script.js).
I then tried creating a reverse-proxy config to proxy /support to /, which sadly put nginx in an infinite loop until it ran out of worker threads:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I'm confused why requests are going into a reverse-proxy loop? Shouldn't proxy_pass remove the /support prefix before proxying the request, and therefore it shouldn't be "caught" again by the /support location?
Just a guess.
Do you want to serve something on /?
If not - it is easy:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
Fiddle around with the ending slashes if it does not work (using them - or not - makes often a difference).
Use alias instead of root so that /support is not added to the /var/www/html folder.
Everything gets redirected to /support.
If you want to serve something on / which is different from /support:
Use sub_filter or subs_filter in /support to rewrite your source code links on-the-fly so that they will never use /.
If you have redirects inside your source code (or proxy_pass backend) - you need proxy_redirect and/or Lua to catch and change them on-the-fly.

Server on virtualmin keeps redirecting to wrong website com WP multisite

When I enabled MultiSite Wordpress, it redirected to one of the Virualmin sites
I tried to put it as a subdomain, I registered it as DNS.
As a subdirectory I tried to include it in the NGINX settings.
######### subdirectory #######
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location /layout-1/ {
index index.php;
try_files $uri $uri/ /layout-1/index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
########## use this or this depends on the configuration ######
1 - ### fastcgi_pass unix:/run/php/php7.3-fpm.sock;
2 - ### fastcgi_pass localhost:8009;
########################################################
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 3000;
;
Should the BIND have an external or internal IP in the domain? I use only one IP for all servers, and in BIND all domains are with external IP. (The question is whether it should be external or internal IP).
Would NGINX have any configuration? How to remove the IP and just put (listen IP: 80) instead of (listen 288.218.198.981:80)
But which configuration would work in general? So you can always avoid complex edits ... For example ... After creating an internal subdomain ...
You have to use all your bind directives in nginx in the same manner. You can use the bind directive in nginx in 2 ways:
listen server_ip:80;
listen 80;
The ideea is that you have to use it one way or another everywhere in your nginx vhosts. If you mix them (one nginx vhost uses listen ip:80; and other listen 80;), it will result in some domains not redirecting or loading properly.
If your server has a private ip then use the private ip. If the server has public ip then use the public ip. You just have to decide which syntax you're going to use.
If you have multiple ip addresses assigned to your server I would recommend you to use the listen ip:80; method

Laravel 404 on production server NGINX

I have Laravel with NGINX on my production server, I have added new routes and they work fine on the localhost. But in production, it returns 404 by Laravel.
1. I have restart NGINX but still no result.
2. I look a the route list and they are present.
What is the problem and how can I fix it?
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name www.somesite.jp;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
With me, I have found that localhost often doesn't mind the case on routes, but in production, getting the case correct is important. Same true on Controller file names etc.
Ok, so it is my bad. Problem was that on the server there were different conditions and I was trying to get User::findOrFail($userId) where ID didn't exist and it returns Laravel 404 error!

Using try_files with uwsgi

I am trying to use the nginx try_files directive with uwsgi_pass and having a ton of difficulty.
Basically what I want is for try_files to ask the uWSGI container if the request URI is valid and if not, then serve up the index.html file instead. My nginx config is as follows:
server {
listen 80;
access_log /tmp/nginx.log;
location / {
try_files $uri $uri/ /index.html;
include uwsgi_params;
uwsgi_pass 127.0.0.1:5001;
}
}
But what this does is check the docroot for every request and if its not there, it simply bails and returns the index.html file.
What I want instead is the following:
Request comes in for www.myapp.com
nginx forwards this request onto the uWSGI container
If that is invalid, then return the index.html
Is there a way to 'ask' uWSGI to try the files instead?
What I'm ultimately trying to accomplish here is HTML5 Pushstate with React Router. I'm running a Flask app with a React front-end. If the user refreshes the browser at www.myapp.com/preferences/userid, then I want nginx to forward that to the container and if its invalid, to return the index.
So, after talking with #Chamindu, I realized I was probably going about this the wrong way. I prevented uWSGI from serving my index.html (even though it could) and instead relied on nginx to serve that instead.
server {
listen 80;
access_log /tmp/nginx.log;
location / {
root /var/www/myapplication/;
try_files $uri $uri/ /index.html;
}
location /api {
include uwsgi_params;
uwsgi_pass 127.0.0.1:5001;
}
}

Resources