How can I make a nginx.conf file to return different web pages for different subdomains? - nginx

I want to make my nginx to show a web page for this subdomain: abc.xyz.com and a different web page for this subdomain: abcd.xyz.com. How can I make that?
I tried to change the location block but it didn't work. Please help me.

You should list both domains in server_name directive:
server {
server_name abcd.xyz.com abc.xyz.com;
location /page {
}
location /anothe_page {
}
}
Or create two different servers as follow:
server {
server_name abc.xyz.com;
location /page {
}
}
server {
server_name abcd.xyz.com;
location /anothe_page {
}
}

Related

Nginx - deploying static page

I have a web application deployed on an nginx with the server name example.com under the path var/www/app.
I added another directory, var/www/demo and it only contains a static .html page; is it possible to have it like example.com/demo?
I have already created a file in /etc/nginx/sites-enabled with the following content:
server {
location /demo {
root /var/www;
}
}
But it is not working, can someone help?
Read http://nginx.org/en/docs/http/ngx_http_core_module.html#root
server {
root /var/www/;
location /app {
index index.html;
}
location /demo {
index index.html;
}
}
Folder structure is
/var/www/app/
--- /var/www/app/index.html
/var/www/demo/
--- /var/www/demo/index.html
$# curl localhost/demo/
<html>
it works
</html>

Nginx set root to subfolder

With Nginx, i need the download folder to become the home page of my website, while still allowing the terms.html page to appear at the root. The download folder displays the list of files with the fancyindex and fancyindex_header directives.
I did this: root /var/www/html/download; but I no longer have access to pages outside the download folder like terms.html
does nginx allow this configuration? here is the structure of my directories:
www/
/download/ # fancyindex on + home page
/account/
/index.php
/login.php
/signup.php
/css/
/js/
robots.txt
terms.html
If your directory structure follows your URI structure, you should probably set the root to /var/www/html at the server level and use /var/www/html/download for one location only.
For example:
server {
...
root /var/www/html;
location / {
root /var/www/html/download;
fancyindex on;
}
location /account/ {
... # PHP stuff
}
location /css/ { }
location /js/ { }
location = /robots.txt { }
location = /terms.html { }
The last four blocks can be empty, as they inherit the value of root from the surrounding block. See this document for details.

nginx not rewriting url for opencart

I have an opencart setup and I'm trying to get nginx to rewrite a url, yet I must be doing something wrong, as nothing I try will work. Here is what I have now that is not working. Just want to remove the index.php?route=common/home from the URL
server {
...
location /index.php?route=common/home {
rewrite http://www.site.com/ permanent;
}
}
How about this
server {
rewrite ^/index.php?route=common/home$ / permanent;
}

nginx httpsubmodule to replace html content

I just came across the nginx httpsubmodule. I think it might be what could help me do this.
I have HTML pages which are static and I want to use the uri portion in the output html.
for e.g. my static URL is http://static.mydomain.com/pic1234.html so in the page I want to display the word "pic1234".
I cannot use javascript as I want to generate HTML meta data for the static page.
Can this be done? if so how? I am not able to find any examples. All I found was this http://wiki.nginx.org/NginxHttpSubModule
note that I have no cgi or programs on the static file serving nginx server.
I have created a simple example. Using this configuration, nginx replaces __name__ token in the html page with the file name ("pic1234" in this case) of the requested html:
nginx configuration:
server {
listen 80;
server_name localhost;
root D:/Development/public;
sub_filter_once off;
location / {
if ($uri ~* /(\w+)\.html$) {
set $filename "$1";
}
sub_filter "__name__" $filename;
try_files $uri $uri/ ;
}
}
D:/Development/public/pic1234.html:
<html>
<head>
<title>__name__</title>
</head>
<body>
<h1>Contents of __name__:</h1>
</body>
</html>
You can find the gist here

Why my nginx config for hiding jsp extension doesn't work properly?

I configure my Nginx web server with follow setting, It hides .jsp extension but doesn't appear my page.
location ~ \.(jsp)$ {
proxy_pass http://192.168.2.225:9595;
if ($request_filename ~* ^(.*).jsp$)
{
rewrite ^/(.*).jsp$ /$1 redirect;
break;
}
}
location ~ \.jsp {
return 403;
}
That will make *.jsp inaccessible. If you want them accessible but only via internal rewrites, then change the 'return 403;' to 'internal;'.

Resources