Subdomain setup nginx - nginx

My reviewer24.com.conf file looks like this:
server {
server_name reviewer24.com www.reviewer24.com;
access_log /home/nginx/domains/reviewer24.com/log/access.log combined buffer=32k;
error_log /home/nginx/domains/reviewer24.com/log/error.log;
root /home/nginx/domains/reviewer24.com/public;
location / {
# Enables directory listings when index file not found
#autoindex on;
# Shows file listing times as local time
#autoindex_localtime on;
# Enable for vBulletin usage WITHOUT vbSEO installed
#try_files / /index.php;
}
include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
include /usr/local/nginx/conf/drop.conf;
#include /usr/local/nginx/conf/errorpage.conf;
}
I have created a folder "m" and placed a mobile version of the website in it.
What rule should I add to this .conf file so when you go to: http://m.reviewer24.com it will display content from "m" folder?

I would create a new .conf file specifically your mobile site, e.g. in m.reviewer24.com.conf with a new server block then reload your nginx config.
This way if you need to make specific changes to your subdomain configuration, it's easier to manage. Alternatively, you could append the new server block the into your existing config.
You'll also probably want to delegate where the mobile traffic access and error logs are :)
server {
server_name m.reviewer24.com;
access_log /home/nginx/domains/reviewer24.com/log/access.log combined buffer=32k;
error_log /home/nginx/domains/reviewer24.com/log/error.log;
root /home/nginx/domains/reviewer24.com/public/m;
location / {
# Enables directory listings when index file not found
#autoindex on;
# Shows file listing times as local time
#autoindex_localtime on;
# Enable for vBulletin usage WITHOUT vbSEO installed
#try_files / /index.php;
}
include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
include /usr/local/nginx/conf/drop.conf;
#include /usr/local/nginx/conf/errorpage.conf;
}

Related

Prevent NGINX from serving local index.html instead of passing to proxied server

Found other similar questions, but none seem to work in my circumstance.
I am attempting to proxy from NGINX to an IIS server which is hosting an archived website in its entirety. The site is coded with some hard index.html links and I don't want to go in and modify the site at all.
Any time the site is called with the /index.html in the URL directly it appears that NGINX is not proxying the location, but instead serving out a local index.html page.
Additionally, I am trying to default instead of to the index.html page when no page is entered (i.e. domain only) instead to pass to a default.htm page (set as default in IIS) which provides a disclaimer page that will require reading before continuing on to the original index.html of the website.
This is my nginx configuration file for the site. I do not want to change my overall structure around because it is what multiple sites use. I need a solution that I can add in.
upstream my_backend {
server 10.10.10.102:1011;
include snippets/shared_upstream_settings.conf;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name server.mydomain.com;
include snippets/shared_server_proxy_settings.conf;
location #proxy {
proxy_pass http://my_backend;
}
location / {
satisfy any;
allow 10.16.0.0/24;
deny all;
auth_basic "Authorized Users Only";
auth_basic_user_file secure/.htpasswd;
auth_request /auth-1;
try_files $uri #proxy;
}
(I don't believe any of the includes should matter for this particular issue)
This configuration works for about 15 other sites I have, but none of them apparently have a hardcoded index.html. Until today I never realized that NGINX will not proxy a direct link to index.html. So I need to either disable or work around that "feature" as well as direct no indicated pages to the disclaimer page.
thanks
The $uri argument in your try_files statement instructs Nginx to test for the existence of a file before branching to the #proxy block. There exists a local index.html file that satisfies that test.
You have two options:
Replace the try_files $uri #proxy; line with proxy_pass http://my_backend; as there is no need for a separate location #proxy block.
Or:
If you want to keep the second location block, change the try_files statement to:
try_files __nonexistent__ #proxy;
try_files requires a minimum of two arguments. All arguments before the final argument are filenames to be tested. __nonexistent__ is just one such name that probably does not exist on your file system (and also helps to document the author's intent).

Merge directory listing for 2 different folders nginx

I have 2 folders like folder-a and folder-b and they are shown as part of the Nginx file explorer.
I have my Nginx file below. I have a problem statement like folder-a and folder-b sometimes share common sub-directory with different content like
folder-a->sub-folder->1.txt
folder-b->sub-folder->2.txt
so when the user is viewing folder folder-a->sub-folder or folder-b>sub-folder he should be able to see both 1.txt and 2.txt i.e. it should feel like a unified view. Can I achieve this is nginx?
worker_processes 1;
events { worker_connections 1024; }
http {
include /etc/nginx/mime.types;
server {
server_name localhost;
listen 80;
root /usr/share/nginx/html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
location ~ ^/folder-a/(.+)$ {
error_page 404 #redirect;
}
location #redirect {
return 301 /folder-b/$1;
}
}
}
Not tested but as we can specify the output format of the autoindex there may be solutions / configuration to print create a new directory listing.
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
But it will always "print / show" the current folders content. Given that if you want to combine the filelist of two different folders njs could help here.
Something like a server that is configured with autoindex = on and a useable format like json or xml. njs could query both directories and reconstruct a new output (xml, json).
Not tested just a fancy idea.

How to set up a websites folder so the Url address outputs as a index of directory

Its a WordPress website using a nginx webservers, we created a folder using FTPS to download/upload files into the new directory, and want the output folder to show like the example link below. Anyone would like to help me out thanks a ton!
[https://i.stack.imgur.com/gCaiD.png]
Directory Listing in NGINX
To enable directory listing, it's as simple as adding this:
autoindex on;
To either a site's NGINX config file (e.g. /etc/nginx/sites-enabled/mysite.com), or in your http NGINX block for it to always occur on all your sites. Here's an example site config from this article:
server {
listen 80;
server_name domain.com www.domain.com;
access_log /var/...........................;
root /path/to/root;
// autoindex on; // put it here for site wide listings
location / {
index index.php index.html index.htm;
}
location /somedir { // Or use location to target particular dirs
autoindex on;
}
}
Fixed: Only had add autoindex on; to the mywebsite.com.conf file.
root /var/www/mywebsite.com/htdocs/;
autoindex on;
Thanks Luke!

Nginx root location

On our server we have the /var/www/root/html/web/ directory that contains all the code, and a /var/www/root/html/web/front/ that contains our static frontend code.
Our frontend communicates with the code only via REST API calls, which have the /api/ prefix, so all the calls will be accessible via ourdomain.com/api/products/ , ourdomain.com/api/products/45 and so on. We also have an admin running there, on ourdomain.com/admin
When we want to see the actual frontend, we have to go to ourdomain.com/front in the browser, which is of course not what we want.
We have, among other stuff, this in our config:
root /var/www/html/web;
index index.php index.html;
location /front {
# some magic to make sure the /front folder will not be parsed
index nothing_will_match;
autoindex on;
}
However, what we wish is that if you go to ourdomain.com it will load /var/www/html/web/front/ folder as root, and if you go to ourdomain.com/api/* or ourdomain.com/admin/* it will load the /var/www/html/web/ as root. Is that possible?
NOTE: the /var/www/html/web/front/ folder can be moved somewhere else if needed, to /var/www/html/front/ for example
What you need is the alias directive (instead of root) to rewrite the URI:
root /var/www/html/web/front;
index index.php index.html;
location / {
}
location /api {
alias /var/www/html/web/;
}
location /admin {
alias /var/www/html/web/;
}

nginx - Serve file conditionally without redirection

I'm currently building a multi-domain cms in rails. Since this content is the same until the next change I'd like to do caching via static files.
The public directory with some cached pages of foo.com and baz.com (/ and /asdf in both cases):
public/
assets/
cms.css
sites/
foo.com/
assets/
screen-some-hash.min.css
index.html
asdf/
index.html
baz.com/
assets/
screen-some-hash.min.css
index.html
asdf/
index.html
What I want to do is the following:
redirect www to non-www (works)
If the requests contains a subdomain (cms, admin, whatever):
If the path contains /assets serve the file in public/assets and set the expire stuff to 30d or so. No problem here since /assets = public/assets and public/ is the passenger root.
Everything else: handle it via rails, no special caching or anything required.
For all other requests (meaning no subdomain):
If the path contains /assets serve the file in public/sites/$host$request_uri and set the expire stuff to 30d or so. Everything else: check for public/sites/$host$request_uri or fall back to the rails app.
I have never worked with nginx conditionals other than the www/non-www redirects and don't really know what I have to do for the conditions mentioned above. If at all possible, I don't want to use redirects for the cached stuff (ie redirection to /sites/foo.com/asdf), instead I'd like to have nginx serve this file directly when going to http://foo.com/asdf.
Further: I don't want to hardcode the hostnames as I'd like to handle an unknown amount of domains. I also don't want to use more than a single rails application for this.
Got something that works, not 100% but good enough for now.
server {
listen 80;
server_name *IP*;
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent;
}
location ~ ^/(assets)/ {
try_files /sites/$host$uri $uri #passenger;
root /home/cms/app/current/public;
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
try_files /sites/$host$uri/index.html /sites/$host$uri $uri #passenger;
root /home/cms/app/current/public;
}
location #passenger {
access_log /home/cms/app/shared/log/access.log;
error_log /home/cms/app/shared/log/error.log;
root /home/cms/app/current/public;
passenger_enabled on;
}
}
For subdomains, this should do the trick:
server {
server_name ~^(?<subdomain>.+)\.example\.com$;
access_log /var/log/nginx/$subdomain/access.log;
location /assets {
expires max;
}
location / {
proxy_pass http://your_rails_app;
}
}
Not really sure about the proxy_pass setting as my only experience with Ruby apps is Gitlab, which I'm running this way. I hope this helps at least a little.
server {
server_name example.com;
location /assets {
root /public/sites/$hostname/$request_uri;
expires max;
}
}
You'll have to add your own settings and play with it a little as I don't have a chance to actually test it now. But it should show you the way.

Resources