Nginx config file - nginx

How does nginx figure I have a conflicting server name?
[warn] conflicting server name "" on 0.0.0.0:80, ignored nginx.
I wouldn't care, except it only seems to be serving files from project.ca and completely ignores projectfinancial.com...
server {
root /var/www/project/infrastructure/project/static-sites/projectlabswebsite;
index index.html
server_name projectfinancial.com *.projectfinancial.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
server {
root /var/www/project/infrastructure/project/static-sites/project-website;
index index.html
server_name project.ca *.project.ca;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}

The answer is:
There is a missing semi colon after index index.html
And for those interested, you can set server_name to .project.ca instead of project.ca *.project.ca as they are both the same.

Related

Nginx Forbidden 403

I'm trying to run a moodle application along with nginx but when I access the domain it returns 403.
I've already changed the folder's permissions, the index.php file exists.
Log Nginx:
*306 directory index of "/var/www/html/moodle/" is forbidden
File config nginx:
server {
#access_log logs/yourwebsite.com-access_log;
#error_log logs/yourwebsite.com-error_log crit;
server_name estudar.cresceredu.com.br www.estudar.cresceredu.com.br;
index index.php index.html index.htm; # index defined to be served under directory
root /var/www/html/moodle/; # default directory where the files will be stored and served from
error_page 404 /index.html;
location = /index.html {
root /var/www/html;
internal;
}
location / {
#root /var/www/html/moodle;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
# moodle rewrite rules
# rewrite ^/(.*.php)(/)(.*)$ /$1?file=/$3 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
Please check if NGINX needs a specific right to do this.
Had the same problem one time also. Fix for me was to set the folder group/user to a other user. For me the solution was the user and group:
www-data www-data
this can be done with the following command:
sudo chown -R www-data:www-data /var/www/html/moodle/
I hope this will fix it for you !

Reverse proxy nginx to itself

I am currently hosting a single-page react app that is hosted in the URL root like so:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I need to put the site behind an AWS elastic load balancer and at the same time change the path so everything is within a /support directory e.g. http://example.com/index.html -> http://example.com/support/index.html.
AWS ALBs do not support URL rewriting so I have to do this within the nginx config on the server. First of all I tried changing the config to:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
This sort-of works but the URLs within the javascript content don't contain the /support path (e.g. they contain http://example.com/script.js instead of http://example.com/support/script.js).
I then tried creating a reverse-proxy config to proxy /support to /, which sadly put nginx in an infinite loop until it ran out of worker threads:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I'm confused why requests are going into a reverse-proxy loop? Shouldn't proxy_pass remove the /support prefix before proxying the request, and therefore it shouldn't be "caught" again by the /support location?
Just a guess.
Do you want to serve something on /?
If not - it is easy:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
Fiddle around with the ending slashes if it does not work (using them - or not - makes often a difference).
Use alias instead of root so that /support is not added to the /var/www/html folder.
Everything gets redirected to /support.
If you want to serve something on / which is different from /support:
Use sub_filter or subs_filter in /support to rewrite your source code links on-the-fly so that they will never use /.
If you have redirects inside your source code (or proxy_pass backend) - you need proxy_redirect and/or Lua to catch and change them on-the-fly.

nginx to always serve the root index.html in every path

I have currently the code below. I am wondering if it's possible to still service this root even though I go to other pages like http://localhost/dog. The problem with my command below is it will return 404
server {
listen 80;
server_name localhost;
location / {
root /usr/src/app/angularjs/dist;
}
}
It is possible. Add the try_files directive to your location block, this will tell nginx to load all requests that cannot be matched to a filesystem path with your index.html:
try_files $uri /index.html;

Nginx configuration for displaying contents in home directory

The requirement is to display contents in local user's home directory when you enter /~user.
www.blahblahblah.com/~user
I've created a local user with home directory and configured Nginx like that:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm test.html;
# Make site accessible from http://localhost/
server_name localhost;
location ~* /\~(.*)/$ {
root /home/$1/;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
But I still got 404 not found error.
The problem is that the root and the URI are concatenated to obtain the request filename, so you are asking nginx to deliver root /home/user/~user/.
The solution is to use a rewrite ... break to sanitise the URI. You can extend the regular expression to capture both elements and then use the named captures in a root and a rewrite ... break statement:
location ~* /\~(?<username>\w+)(?<userpath>/.*)$ {
root /home/$username;
rewrite ^ $userpath break;
}

nginx Redirect subfolder to root domain

I want to redirect the subfolder and all contents to root domain.
For example:
http://www.example.com/ubb/ will redirect to http://www.example.com
My server configuration is like below:
server {
listen 80 default_server;
root /home/vishant/devcenter/wava-v1.1/HTML;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name baetter.l;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#proxy_pass "http://127.0.0.1:3000";
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
i have found similar problem solved using htaccess here
But how can i achieve in nginx??
One of a number of solutions is:
location ^~ /ubb/ {
return 302 /;
}
The ^~ modifier ensures that this prefix location continues to take precedence if you were to add any regex locations in the future. See this document for details.
The return directive is documented here.

Resources