I have a website which is an app containing other apps.
In the main app, I'll load the other apps with iframes.
The main app is served at the domain root: http://example.com
The other apps are served under the /apps path:
http://example.com/apps/app-a
http://example.com/apps/app-b
Also the apps are developed in Polymer so there is client side navigation. I make sure the /apps path is not used on the client side to hit the NGINX server correctly but it also means that for url such as http://example.com/view1 it should redirect to the index.html of the main app. And for url like http://example.com/apps/app-b/view1 it should redirect to the index.html of app-b.
I'm trying to configure NGINX to serve those static apps.
server {
listen 80;
server_name example.com;
root /var/www/example.com/main-app;
index index.html;
try_files $uri $uri/index.html /index.html;
location ~ /apps/([a-z-]+) {
alias /var/www/example.com/apps/$1;
}
}
With the above config I have the main app working with the correct redirection to the index.html for the /view1 path for example.
But I have a 403 forbidden error for the sub apps.
directory index of "/var/www/example.com/apps/app-b" is forbidden,
client: 127.0.0.1, server: example.com, request: "GET /apps/app-b/
I've tried other configurations with no success (infinite redirection leading to /index.html/index.html/index.html...).
I'm sure about the filesystem permissions all directories are 755 and files are 644.
I don't know why it is trying to do a directory index.
Any help is appreciated.
When using alias with a regular expression location, you need to build the entire path to the file. Currently, you are only capturing the second path element.
Having said that, you do not actually need to use an alias here, as the root directive can be use instead.
location /apps/ {
root /var/www/example.com;
}
See this document for details.
Related
I am building the angular app and want to route the URL to http://localhost/sample/AngularApp/. but don't want to give the entire URL in the browser. I will give the only localhost. In angular, while building the dist we are using the command ng build --base-href=/sample/AngularApp/ and created the folder structure /sample/AngularApp in Nginx mentioned path /usr/share/nginx/html.so while accessing the application still, we are giving the entire path in browser. so, How could I resolve this issue and how to configure that path in nginx.conf file to autoroute the URL?
I would go with standard configuration:
server {
listen 80;
listen [::]:80;
root /var/www/html/YourAngularApp/dist;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
This gives opportunity to serve other files (if found) or fall back to index.html located at the project's root.
Now, if you have everything in place you should build your project with
ng build --base-href=/
--base-href=/ can be omitted as it defaults to /. Please have a look at Angular docs about using --base-href and --deploy-url
I have a domain (let it be example.com). Also, I have a configured nginx web server, where example.com is pointed to the root directory /var/www/example.
Then, I wanted to point service.example.com to /var/www/example, and then point the original example.com to /var/www/service
Fisrt lines of nginx config for service.example.com look like this:
server {
server_name service.example.com;
root /var/www/example;
......
It works, and when I try to access service.example.com, I'm being redirected to the index file in /var/www/example.
Then, I made a config for example.com:
server {
server_name example.com www.example.com;
root /var/www/service;
......
,but when I'm trying to access example.com, I see the index file of /var/www/example— as if I tried to access service.example.com.
So, how can I solve this problem, and see index file of /var/www/service when I'm trying to access example.com?
Problem was in symlinks.
Some of them was not the symlinks in sites-enabled, but archive. So, nginx didn't handled them.
Problem was solved!
server {
root /var/www/service;
server_name service.example.com;
}
reference: https://hackprogramming.com/how-to-setup-subdomain-or-host-multiple-domains-using-nginx-in-linux-server/
I'm having issues serving pictures with nginx. I originally started with a Django project and I wanted to serve the user uploaded media files via nginx but I wasn't able to get it working no matter what I tried.
So, I've make a second temporary droplet/server and am trying a bare bones setup with no Django project, just Nginx, to see if I can get it to simply serve an index and a couple pictures in a folder called 'media'. Here is the server block:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html;
server_name 159.89.141.121;
location / {
try_files $uri $uri/ =404;
}
location /media/ {
root /var/www/example.com/media;
}
}
Now, the index.html is served fine but if I try to go to 159.89.141.121/media/testpic.jpg it still doesn't work and returns a 404 error. I'm at a complete loss here, I've tried using alias instead of root, I've tried changing the folder structure and setting all permissions to 777 and changing folder and file ownership, permissions shouldn't be a problem because the index.html works fine with the same permissions; I just cant seem to figure out what I'm doing wrong. The picture is in the folder but nothing I try allows me to access it via the uri. Are there any obvious problems with my server block?
Well I decided to read the documentation and realized that the location block adds to the root directory specified..
So the pathing of
`location /media/ {
root /var/www/example.com/media;
}`
would end up routing example.com/media/testpic.jpg to /var/www/example.com/media/media/testpic.jpg
I've changed the location block to look like this
location /images/ {
root /var/www/example.com/media;
}
and it will now route example.com/images/testpic.jpg to /var/www/example.com/media/images/testpic.jpg
I'm not sure why it didn't work when I tried the alias directive, though...
I have some slate docs as website and would like to serve them on the internal server, through a subdomain as follows: internal-docs.mysite.com. For the record, accessing mysite.com shows the "nginx is running propertly" page.
I've created a config file with following path and name: /etc/nginx/sites-available/internal-docs.mysite.com:
server {
listen 80;
server_name internal-docs.mysite.com;
root /var/www/docs-internal;
index index.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
}
And of course, I've put the files in /var/www/docs-internal. And then I made a symlink to the uppershown config file in the /etc/nginx/sites-enabled dir:
internal-docs.mysite.com -> ../sites-available/internal-docs.mysite.com
Then I reload nginx -s reload but "this site can't be reached" error is what I get when accessing the URL.
The setup and configuration look correct to me (according to the guidelines I've followed), so that's why I'm in a dead end, sort of...
It seems you forgot the Listen directive. Try the following:
server {
listen 80;
server_name internal-docs.mysite.com;
root /var/www/docs-internal;
index index.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
}
If that does not work, check:
That Nginx user has read permission to the site content. For example if your Nginx user is www and you have root access, do the following:
# su www
$ cat /var/www/docs-internal/index.html
If that fails, ensure the location has correct ownership and permissions. Note that for a user to be able to browser a directory, that directory must have the execute bit for that user or user group.
That Nginx user has read permission on file ../sites-available/internal-docs.mysite.com. For example if your Nginx user is www and you have root access, do the following:
# su www
$ cat /etc/nginx/sites-available/internal-docs.mysite.com
If that fails, ensure that the config files have correct ownership. Note: normally Nginx master process is run by root, and that process spawns sub-processes run as Nginx user, so permissions on config files are unlikely to be the problem.
That maybe your config file name should end with ".conf" (on my server I have the following line: include conf.d/*.conf; so it will NOT load any conf file ending with ".com".
That Nginx tries to load files in ../sites-available/ in its main config file. Maybe it does not and looks instead in the conf.d directory (the default).
That you can do a ping and nslookup on the subdomain. If you cannot, then you have to fix that first (DNS, firewall...).
For the sake of others - the configuration I wrote was correct, and my problem was in 2 things:
I had to remove the listen 80 directive, since there is another configuration file already, that specifies that nginx should listen on port 80. One should not tell nginx twice to listen on the same port, even if it's in two separate configuration files
Permissions on the /var/www/docs-internal folder. Opening a folder requires x (execute) permissions, while opening a file requires r (read) perm. I had to provide the according permissions to all the folders in this hierarchy, so that the content could be open globally (from everyone), which is basically accessing it from the browser.
I have a website running on port 80 as a default_server, its root in the config file is root /var/www/sample1/current and I can access it through www.sample1.com, and I have one more website on the same server:
/var/www/sample2/current
So I need to call the second website /var/www/sample2/current through www.sample1.com/sample2 on the same port.
You would want to use the http://nginx.org/r/alias directive.
root /var/www/sample1/current;
location ^~ /sample2 {
alias /var/www/sample2/current;
}