I got the following domain configuration on my nginx server (nginx 1.2.1):
server {
listen 80 default_server;
server_name example.com;
root /var/www/example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location ~ /content/(.*)\.(txt|md) {
return 301 /error;
}
location ~ /tpl/(.*) {
return 301 /error;
}
location ~ /sys/(.*) {
return 301 /error;
}
location / {
index index.php
try_files $uri $uri/ /index.php;
}
}
As you can see, I want to disallow access to all .txt/.md files in my content directory as well as every access to my /tpl and /sys directories. But, nginx seems to ignore each one of these rules.
So I tried something different, i.e.:
location ^~ /templates {
return 404;
}
As this works, I guess there's a precedence-problem. Does anybody know how the apply all 3 rules?
Thanks!
— Max
Related
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I have a nginx version: nginx/1.10.3 (Ubuntu) running on Ubuntu 16.04.2 LTS.
I use nginx to serve static files, bundles generated by webpack, but that's irrelenvant.
What I want to achieve is this:
On example.com I want to serve /home/bundles/main/index.html. I can do this.
On projects.example.com/project_1 I want to serve /home/bundles/project_1/index.html.
On projects.example.com/project_2 I want to serve /home/bundles/project_2/index.html.
The last two, I can't do. When I go to projects.example.com/project_1 or projects.example.com/project_2 I am served the default nginx page.
To make things more confusing /etc/nginx/sites-enabled/default is entirely commented out.
Additionally, if in the location block of projects.example.com I replace, for example, project_1 with /, I will be served that specific project, but then I will have no way of serving the other.
Bellow, I will show you my nginx configuration
server {
listen 80;
server_name example.com;
location / {
root /home/bundles/main;
try_files $uri /index.html;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
location / {
root /home/bundles/main;
try_files $uri /index.html;
}
ssl_certificate ...
ssl_certificate_key ...
}
server {
listen 80;
server_name projects.example.com;
location /project_1 {
root /home/bundles/project_1;
try_files $uri /index.html;
}
location /project_2 {
root /home/bundles/project_2;
try_files $uri /index.html;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name projects.example.com;
location /project_1 {
root /home/bundles/project_1;
try_files $uri /index.html;
}
location /project_2 {
root /home/bundles/project_2;
try_files $uri /index.html;
}
ssl_certificate ...
ssl_certificate_key ...
}
Thank you for your help!
EDIT
My Answer
The solution I found was to change the root with alias.
server {
listen 80;
server_name example.com;
location / {
root /home/bundles/main;
try_files $uri /index.html;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
location / {
root /home/bundles/main;
try_files $uri /index.html;
}
ssl_certificate ...
ssl_certificate_key ...
}
server {
listen 80;
server_name projects.example.com;
location /project_1 {
alias /home/bundles/project_1;
index index.html;
}
location /project_2 {
alias /home/bundles/project_2;
index index.html;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name projects.example.com;
location /project_1 {
alias /home/bundles/project_1;
index index.html;
}
location /project_2 {
alias /home/bundles/project_2;
index index.html;
}
ssl_certificate ...
ssl_certificate_key ...
}
The solution is based on these two answers. The first answer showing how to solve the problem and the second answer providing a explanation as to why alias works and root does not.
To quote #treecoder
In case of the root directive, full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.
In my particular case, this would translate like this;
With root, the path nginx would try to access would be /home/bundles/project_1/project_1.
With alias it accesses the correct path, /home/bundles/project_1.
Going back one level, for example, saying:
root /home/bundles/ is not really a option either. That is because my projects are not actually called project_1 and project_2. The actual structure is more similar to this.
In /bundles I have the directories project_a and project_b. I want to route project_1 to the project_a directory and project_2 to the project_b directory.
That is why I used alias.
I hope this helps.
You have:
location /project_1 {
root /home/bundles/project_1;
try_files $uri /index.html;
}
So the root is only defined for URIs that begin with /project_1. For any other URI, the default root will be used.
If you present the URI /project_1/ (with a trailing /), assuming that the default index directive is in force, nginx should return your /project_1/index.html content.
However, the URI /project_1 is not found - so /index.html is returned instead. The URI /index.html does not begin with /project_1, so the default root is used.
If you want the URI /project_1 to work as expected, and the default action to go to the project's index.html file, change the try_files directive.
location /project_1 {
root /home/bundles/project_1;
try_files $uri $uri/ /project_1/index.html;
}
See this document for more.
As both projects share a common root, you could simplify as follows:
server {
listen 80;
server_name projects.example.com;
root /home/bundles
index index.html;
location /project_1 {
try_files $uri $uri/ /project_1/index.html;
}
location /project_2 {
try_files $uri $uri/ /project_2/index.html;
}
location / {
deny all;
}
}
I added the index directive to avoid relying on the default value (which is the same), and a location block to deny access to areas outside of the projects.
I had the same problem...and it turned out, for me, that the default site that was "overtaking" my desired secondary site and serving the default files...well it allowed IP6 requests, and my new site didn't.
Here's an example of my access.log:
::1 - - [05/Sep/2020:15:05:16 -0600] "GET / HTTP/1.1" 200 40 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
Notice the ::1 bit? That's an IP6 request that I guess my browser was defaulting to. So I just made sure to enable local IP6 requests for the site in question in my site config file by changing:
listen 80;
server_name example.local;
to:
listen 80;
listen [::]:80;
server_name example.local;
That was it.
I'm starting to go nuts at this. For some reason, routing wont work on my single page application. So www.example.com works, but not www.example.com/service. I read a lot of posts on how to fix it, but nothing seems to work.
This is my config file at /etc/nginx/conf.d/App.conf
server {
listen 80;
server_name example.com *.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /path/to/certificate/;
ssl_certificate_key /path/to/certificate/key;
root /var/www/App/public;
index index.html;
location / {
try_files $uri /index.html;
}
ssl_session_timeout 5;
}
I have tried all kind of different "location" routes, and nothing seems to work. I do also restart the service with "sudo service nginx restart" everytime I change.
Any clues?
In the comments you said there's a small fixed set of possible routes. In that case you can add a location block for each route, with an alias to the top-level, for example,
location / {
try_files $uri $uri/ =404;
}
location /services {
alias /var/www/App/public;
try_files $uri $uri/ =404;
}
Edit: Or, if you want to serve the top-level index.html in response to any request at all,
location / {
try_files /index.html =404;
}
I ham trying to make http://example.com serve http://example.com/home.html from /home/ubuntu/mysitedir/home.html.
I have the following conf file which successfully redirects everything to https, and proxies to uwsgi. The http->https redirection works fine, and the uwsgi proxy works, but http(s)://example.com/, http(s)://example.com/home.html, http(s)://example.com/index.html, and http(s)://example.com/index.htm are all 404
Any pointers as to what I can try?
Here is my conf file:
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
root /home/ubuntu/mysitedir/;
index home.html;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example_combined.crt;
ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
root /home/ubuntu/mysitedir/;
index home.html;
location /images/ads {
alias /home/ubuntu/mysitedir/images/ads/;
}
location /images {
alias /home/ubuntu/mysitedir/images/;
}
location /static {
alias /home/ubuntu/mysitedir/static/;
}
location / {
alias /home/ubuntu/mysitedir/;
include uwsgi_params;
uwsgi_pass unix:/tmp/mysitedir.sock;
}
}
Thanks.
location / is the catch-all. You could try the try_files directive like this:
location #wsgisite {
include uwsgi_params;
uwsgi_pass unix:/tmp/mysitedir.sock;
}
location / {
index home.html;
try_files $uri $uri/ #wsgisite;
}
Any thing coming into the base location will first check to see if it is a file, or a directory with an index (home.html) file in it, and if not, then pass it on to the #wsgisite.
This should also make the other three location directives obselete, since nginx will be checking for the files first before passing it to the wsgi block.
I actually have this working, but I'd like to know if I am doing it the most efficient way, or if there are any improvements I can make to my conf file. Here is what I am attempting to do:
If any file is requested from the root, we should always serve "index.html". No other file should be accessible, and requesting anything else should be treated as if you requested "index.html". Currently I'm using rewrite, but a redirect would be okay too, and possibly preferable.
Any file under "/css" or "/js" can be requested, and requesting files from those directories that don't exist should return a 404.
Here's my current working conf file:
server {
listen 80;
server_name www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
location = /index.html {
}
# map everything in base dir to one file
location ~ ^/[^/]*$ {
rewrite ^/[^/]*$ /index.html;
}
location ~ ^/css/ {
}
location ~ ^/js/ {
}
}
UPDATE
My final conf file, which is both faster under a load test and simpler than the original, is here:
server {
listen 80;
server_name example.com;
root /var/www/register;
location = /index.html {
}
# Default location, request will fallback here if none other
# location block matches
location / {
rewrite ^.*$ /index.html redirect; # 'root' location '/'
}
location /css/ {
}
location /js/ {
}
}
I'm not sure if I got this right or not, but check this answer, you always want to server index.html so it should be the default location location /
server {
server_name example.com www.example.com;
client_max_body_size 50M;
root /var/www/mysite;
index index.html;
location / {
try_files index.html =404;
}
location /(css|js) {
try_files $uri =404;
}
}