Why nginx not able to serve sites in different locations? - nginx

I want to use nginx to host some static html which is located in different path on my pc, below is my configuration:
server {
listen 8080;
server_name localhost;
location /chatserver {
root /Users/xxxx/gitrepo/chatserver/public;
index index.html;
}
location /test {
root /Users/xxxx/test/test_site;
index index.html;
}
The file structure is:
Users
|-xxxx
|-gitrepo
| |-chatserver
| |-public
| |- index.html
|-test
|-test_site
|- index.html
But when I access: http://localhost:8080/chatserver or http://localhost:8080/test, nginx always responds 404 Not Found.
If I access: http://localhost:8080/, nginx will return the default nginx welcome page.
Why is my configuration not working?

I think you should use alias instead of root.
In your example the URL /chatserver/index.html will search the /Users/xxxx/gitrepo/chatserver/public/chatserver/index.html (note the "undesired" chatserver after public!). Check your nginx's logfiles!
See documentation of root and documentation of alias.

Related

How do I configure nginx correctly to work with my Sinatra app running on thin?

I have a Sinatra app (app.rb) that resides within within /var/www/example. My setup is nginx, thin, and sinatra.
I have both nginx and thin up and running but when I navigate to my site, I get a 404 from nginx. I assume that the server block config is wrong. I've tried pointing root to /var/www/example/ instead of public but that makes no difference. I don't think the request makes it as far as the sinatra app.
What am I doing wrong?
Server block:
server {
listen 80;
listen [::]:80;
root /var/www/example/public;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
config.ru within /var/www/example directory:
require File.expand_path('../app.rb', __FILE__)
run Sinatra::Application
config.yml within /var/www/example directory:
---
environment: production
chdir: /var/www/example
address: 127.0.0.1
user: root
group: root
port: 4567
pid: /var/www/example/pids/thin.pid
rackup: /var/www/example/config.ru
log: /var/www/example/logs/thin.log
max_conns: 1024
timeout: 30
max_persistent_conns: 512
daemonize: true
You have to tell nginx to proxy requests to your Sinatra application. The minimum required to accomplish that is to specify a proxy_pass directive in the location block like this:
location / {
proxy_pass http://localhost:4567;
}
The Nginx Reverse Proxy docs have more information on other proxy settings you might want to include.

Nginx location not resolved

I am trying to have my nginx instance host from two separate static files.
The config for root path works but using any other prefix pattern gives 404 not found.
The config is as follows:
server {
listen 80;
server_name *.saurabhharwande.com;
root /var/www/certbot;
location /abc/ {
index vindex.html;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
www.saurabhharwande.com Works
www.saurabhharwande.com/abc Gives 404
Am I missing something?
Edit: My basic understanding of how nginx configuration works was flawed. I thought that index file is searched in the root itself /<domain>/<root>/index.html and location is just used to point to different roots. It is rather searched at <domain>/<root>/<location>/index.html

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 two websites with nginx

I want to run a simple nginx page that serves two pages. One from folder ~/A and one from ~/B
Each folder runs a copy of Python's SimpleHTTPServer in ports 1000 and 2000
Each file has a single file called index.html with text Hello World!
server {
listen 80;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
root ~/A;
proxy_pass http://localhost:1000;
}
location /B/ {
root ~/B;
proxy_pass http://localhost:2000;
}
}
Unfortunately curl http://localhost/B/index.html returns a 404.
<head>
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code 404.
<p>Message: File not found.
<p>Error code explanation: 404 = Nothing matches the given URI.
</body>
What is wrong with my nginx conf file? Why can't it route properly?
I think you want use alias ~/B instead of root ~/B because your location /B/ will try ~/B/B. See alias and root documentations.
you can open the nginx debug log and s.
i think this url will matches 'location \' and goto A.

How does one map many URLs to a single file using nginx?

I have a static file, index.html. How would I configure nginx to serve it from every path on the domain?
URL | file
-----------------
/ | index.html
/foo | index.html
/bar | index.html
/baz | index.html
Essentially, I want a wild card match.
(I realize this will be an unusual setup.)
I faced the same problem a while back and seem to remember doing something along the lines of:
server {
server_name example.com www.example.com;
root /var/www/vhosts/example.com;
location / {
try_files index.html =404;
}
}
If you don't mind returning an error code (e.g. you're down for maintenance) you could also do something like:
server {
server_name example.com www.example.com;
root /var/www/vhosts/example.com;
location / {
error 503 index.html;
return 503;
}
}
Is this what you're looking for?
rewrite ^(.*)$ index.html

Resources