Want to load same name file from Folder nginx server - nginx

I am new to nginx server. I want to load same name file from folder. Suppose:
I have file system as:
folder/xyz/xyz.html
I am want to access xyz.html file using this url: http://localhost:84/xyz using nginx server. I have created the configuration like as:
server {
rewrite_log on;
listen :84;
server_name localhost;
root folder;
location / {
try_files $uri $uri/index.html /index.html;
}
}
With this configuration I am successfully accessible the folder/xyz/index.html file. But I want to access folder/xyz/xyz.html file.
NOTE: name ("xyz") will be dynamically.
Can any please help me.

Related

.well-known/acme-challenge nginx 404 error

I'm trying to verify a file upload for SSL certificate.
The file needs to be .well-known/acme-challenge/file
I have successfully placed the file as above, but while accessing the same file from the web http://weburl.com/.well-known/acme-challenge/file, 404 error is coming up.
When I place the same file in .well-known/ the file can be access from the path http://weburl.com/.well-known/file successfully.
My nginx configuration:
server {
listen 80;
server_name weburl.com;
root /var/www/html;
location ~ /.well-known {
allow all;
}
location ~ /\.well-known/acme-challenge/ {
allow all;
root /var/www/html;
try_files $uri =404;
break;
}
}
You have to grant permissions for www-data user.
sudo chown -R www-data:www-data .well-known
In the first case it looks for /var/www/html/.well-known/file.
In the second case it looks for /var/www/html/file.
What you intend is for it to find /var/www/html/.well-known/acme-challenge/file
This is because you specify root in the location block, which changes where it reads the file from.
So instead of this:
location ~ /\.well-known/acme-challenge/ {
allow all;
root /var/www/html; # <================= Your problem, sir
try_files $uri =404;
break;
}
You should have this:
location ~ /\.well-known/acme-challenge/ {
allow all;
try_files $uri =404;
break;
}
Shameless plug: If you're just doing simple virtual hosting and you're familiar with node at all you might like Greenlock.
If you have installed the LetsEcnrypt module on Plesk, but for some reason you need to authorize for eg. example.com manually like we do.
Add you authorization code to
/var/www/vhosts/default/htdocs/.well-known/acme-challenge
instead of expected (domain webroot)
/var/www/vhosts/example.com/htdocs/.well-known/acme-challenge
To find so I had to check /var/www/vhosts/system/example.com/conf/httpd.conf

Change Nginx root directory to workspace folder path

Issue
I installed Nginx with brew on Mac OSX. Then I modified /usr/local/etc/nginx/nginx.conf as below and got a server 500 error:
server {
listen 666;
server_name localhost;
root /Users/username/Desktop/workspace/projectname/dist;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
}
What I tried
I knew that brew install default root is /usr/local/var/www so I was able to get it working by paste dist folder into /usr/local/var/www and update Nginx config like this:
server {
listen 666;
server_name localhost;
root /usr/local/var/www/dist;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
}
Other issue
Even after I paste dist folder into /usr/local/var/www I still got 500 error by updating root like the followings:
root /dist
root dist
root ./dist
Why does default
root index.html
gets displayed when I first run Nginx ? but I have to specify my dist folder with full path like /usr/local/var/www/dist ? Can someone please explain.
Goal
I want to be able to change root directory to my dist folder in workspace so I don't need to paste the dist folder into /usr/local/var/www every time after I rebuild.
I'm going to assume permissions is the problem. nginx runs as user "www-data" unless you change it. Your directories and files need to allow that user and have their directories set as 755 and the files set to 644.

nginx server block not playing nicely - 'server not found'

The basic installation is working, on linux mint OS. resolving the domain on 'localhost' confirms that nginx is running.
however, the issue i am running into stems from the generation of my own server block. its very basic:
server {
listen 80;
listen [::]:80;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from alias.
server_name tokum.com www.tokum.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
as you can see, i have created an alias for www.tokum.com in this server block. attempting to resolve this url in a browser, i am greeted with the lovely 'server not found' message.
my feeling is that it surrounds the 'try_files' functionality, but i cannot be sure why.
No other resources have been created on the server other than my tokum.com server block file, which is located at the path /etc/nginx/sites-available/tokum.com. Any help is most appreciated.

nginx serving static files from root and uploaded files from alias

I run nginx as a reverse proxy server in front of apache.
I need to access uploaded files from frontend in backend so the way to go is to use an alias in nginx site config but static files in backend should be handled directly by nginx. I'm new to nginx so here is my partial config that handles static files. I also specified an alias (/images) but it will not work because it is overwritten by second condition.
How can the two conditions be combined so that nginx handles static files from root (backend app) and uploaded files from frontend app.
In apache config I included an alias for this problem and it works but without nginx in front.
Here is my partial nginx config:
server {
listen 80;
root /var/www/website/backend/www;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm;
server_name admin.website.com www.admin.website.com;
location / {
proxy_pass http://localhost:8080;
include /etc/nginx/proxy_params;
}
#The alias to handle uploaded files(.jpeg, .pdf) from frontend
location /images {
alias /var/www/website/frontend/www/images;
}
#let nginx handle static files from root
location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
expires 30d;
}
.
.
.
}
The regular expression location block takes precedence over a prefix location block (unless the ^~ modifier is used). See this document for details.
Try:
location ^~ /images {
root /var/www/website/frontend/www;
}
Note that the root directive is preferred in this case (see this document for details)

Serving static HTML files in Nginx without extension in url

root directory = /srv/myproject/xyz/main/
in the "main" folder I have few *.html files and I want all of them to point at a url say /test/ (which is quite different from the directory structure)
this is my very basic nginx configuration
server {
listen 80;
error_log /var/log/testc.error.log;
location /test/ {
root /srv/myproject/xyz/main/;
#alias /srv/myproject/xyz/main/;
default_type "text/html";
try_files $uri.html ;
}
}
If I use simple alias
location /test/ {
alias /srv/myproject/xyz/main/;
}
then its work perfectly, I mean I can access those html files by http://www.myurl.com/test/firstfile.html and so on
but I dont want that html extension.
I tried to follow these threads but no success
http://forum.nginx.org/read.php?11,201491,201494
How to remove both .php and .html extensions from url using NGINX?
how to serve html files in nginx without showing the extension in this alias setup
Try this
location ~ ^/test/(.*)$ {
alias /srv/myproject/xyz/main/;
try_files $1.html =404;
}

Resources