Nginx error: rewrite or internal redirection cycle while internally redirecting to "/index.md" - nginx-config

I am trying to serve a vue app that has an index.md file as the root file. here is my nginx config:
server {
listen 7072;
listen [::]:7072;
root /home/dave/web/dist;
index index.md index.htm index.html index.php;
server_name 0.0.0.0:7072;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
root /home/dave/web/dist;
try_files $uri /index.md;
}

Related

Allow app to be loaded from a folder path in NGINX

I have an Angular application deployed in Nginx in /usr/local/etc/nginx/html location.
The app is working fine if I go to http://localhost:8000/#/search
I want to load the application as http://localhost:8000/lib/<company>/app/#/search as the lib/<company>/app portion is essential to get the company name from the URL.
I tried setting alias for /lib/(.*?)/app location in nginx.conf, which gives 404 for the path.
listen 8000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/etc/nginx/html;
index index.html index.htm;
}
location /lib/(.*?)/app {
alias /usr/local/etc/nginx/html;
}
is there a way I can load the app and preserve the full URL in the browser?
Thanks.

Nginx HTTPS subdomain redirect

I have a couple of websites configured with Nginx (nginx/1.10.3 (Ubuntu)) Server Blocks. Browsing to any of the configured domains listed below works as expected:
Those that are configured to use HTTPS get automatic HTTP -> HTTPS redirects (morgrowe.com for example). The issue is when I browse to a subdomain that doesn't exist (for example: doesnotexist.morgrowe.com). If I go to http://doesnotexist.morgrowe.com, I get redirected to the default Nginx page (which is what I want). However, if I go to https://doesnotexist.morgrowe.com, I get redirected to https://api.morgrowe.com.
This wouldn't be so bad, but if I go to https://doesnotexist.carpyslocksmiths.com, I also get redirected to https://api.morgrowe.com.
Where can I find the configuration that makes this happen? Ideally, I'd like the default nginx 404 page to appear like it does for http connections. I imagine I have to configure something in /etc/nginx/sites-available/default, but I don't know where to start. Here's my /etc/nginx/sites-available/default file contents:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
use a Default “Catch All” Server Block using _ in server_name both for http and https. _ is just an invalid value which will never trigger on a real host.
Please see example From below link
https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

How do I redirect all requests to an application running on a specific port using nginx?

I'm running an application on port 8000, and need to redirect all requests coming on port 80 to my application (port 8000). I understand that using nginx is the way to go about it. I modified ngninx.conf as follows:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:8000;
try_files $uri $uri/ =404;
}
}
I'm able to redirect requests to my application server now, but requests of the form xyz.com/abc return 404 not found. I thought the above would match all requests and redirect everything to the application on port 8000. What am I doing wrong here?
try_files checks the existence of a file against the defined root in the server block. What it's doing in the location / block is checking for the file, and then returning 404 when it's not found. Perhaps you wanted your config to look like this instead?:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ #my_proxy;
}
location #my_proxy {
proxy_pass http://127.0.0.1:8000;
}
}
Here, if try_files fails to find anything, it will pass over the request to you app server running on port 8000. If it still returns 404 then the file indeed doesn't exist.

Nginx config for single page app with HTML5 App Cache

I'm trying to build a single page app that utilizes HTML5 App Cache, which will cache a whole new version of the app for every distinct URL, thus I must redirect everyone to / and have my app route them afterward (this is the solution used on devdocs.io).
Here's my nginx config. I want all requests to send a file if it exists, redirect to my API at /auth and /api, and redirect all other requests to index.html. Why is the following configuration causing my browser to say that there is a redirect loop? If the user hits location block #2 and his route doesn't match a static file, he's sent to location block #3, which will redirect him to "/" which should hit location block #1 and serve index.html, correct? What is causing the redirect loop here? Is there a better way to accomplish this?
root /files/whatever/public;
index index.html;
# If the location is exactly "/", send index.html.
location = / {
try_files $uri /index.html;
}
location / {
try_files $uri #redirectToIndex;
}
# Set the cookie of the initialPath and redirect to "/".
location #redirectToIndex {
add_header Set-Cookie "initialPath=$request_uri; path=/";
return 302 $scheme://$host/;
}
# Proxy requests to "/auth" and "/api" to the server.
location ~* (^\/auth)|(^\/api) {
proxy_pass http://application_upstream;
proxy_redirect off;
}
That loop message suggests that /files/whatever/public/index.html doesn't exist, so the try_files in location / doesn't find $uri when it's equal to /index.html, so the try_files always internally redirects those requests to the # location which does the external redirect.
Unless you have a more complicated setup than you've outlined, I don't think you need to do so much. You shouldn't need external redirects (or even internal redirects) or server-side cookie sending for a one-file js app. The regex match for app and api wasn't quite right, either.
root /files/whatever/public;
index index.html;
location / {
try_files $uri /index.html =404;
}
# Proxy requests to "/auth" and "/api" to the server.
location ~ ^/(auth|api) {
proxy_pass http://application_upstream;
proxy_redirect off;
}

dotCloud nginx.conf: how to get the "index index.php" directive to work?

I got auth to work by "pushing" an nginx.conf file in the application directory, so I know the file works, but /app will not trigger /app/php.index. I can't get nginx working on my vista laptop with php and I can't edit the /etc/nginx/nginx.conf in the dotCloud instance as dotCloud makes life difficult by not giving root.
(Note that the .htpasswd is relative to the ngnix.conf file location, nice).
server {
location / {
index index.php;
}
location /admin {
auth_basic "enter password";
auth_basic_user_file .htpasswd;
index index.php;
}
}
You may need an nginx directive to map requests to your dynamic content. This controller can can then route them appropriately.
try_files $uri $uri/ /index.php;
For an example project, see the CakePHP tutorial.