Nginx Forbidden 403 - nginx

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 !

Related

Nginx location rules not applying

I want to run both WordPress and YOURLS on one domain which is configured by a NGINX server block (not the default site). Since both need to handle URLs differently, they need different try_files directives. WordPress sits on the root of the domain (domain.tld), while YOURLS is being installed to the /g/ directory. Despite the two location rules, I get 404s on any links generated by YOURLS (e.g. domain.tld/g/linkname, all are redirects to external URLs), though I can access the admin backend.
As far as I read, declaring to location rules (one for /g/, and one for /) should suffice in order to let NGINX handle the direct and the /g/ URLS differently - is there something in wrong in my thinking?
The try_files rules are correct and do work well on other single-application server block (WordPress as well as YOURLS on installs on separate server blocks).
The server block definition config looks like this:
server {
listen [::]:80;
listen 80;
server_name domain.tld www.domain.tld;
return 301 https://domain.tld$request_uri;
}
server {
listen [::]:443 ssl;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
root /var/www/html/domain.tld;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name domain.tld www.domain.tld;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
}
location /g/ {
try_files $uri $uri/ /yourls-loader.php$is_args$args;
expires 14d;
add_header Cache-Control 'public';
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
The problem with the location /g/ try_files directive is that the path to the YOURLS loader isn't correct. If the URL handler (yourls-loader.php) lies within the /g directory, the path to it has to be changed to include the /g directory:
try_files $uri $uri/ /g/yourls-loader.php$is_args$args;
The location rule does not imply that each path is handled from that location as well, but rather from the root path given above.

nginx Restrict or Redirect requests to go through index.php in the root

My project requirement is to launch an angular(v6)/ionic(v4) app(index.html) from Slim framework (index.php).
The root folder structure is attached in the image below:
'www' is the root directory where the Slim index.php lies. Inside 'www' is the app folder where the angular build files(index.php) are added.
The requirement is that all the requests should go through Slim index.php in the root folder and based on some session logic we have to route or launch the angular app (index.html).
Right now, 'https://domain/' goes through index.php. But,
'https://domain/app/' directly launches the angular app (index.html).
How can I configure nginx so that all requests are to be handled in the root directory by Slim index.php ?
server {
server_name <domain>;
root /var/www/<some name>/public/www/;
index index.php index.html index.htm;
access_log /var/log/nginx/<some name>.log;
error_log /var/log/nginx/<some name>.log;
sendfile off;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ index.php /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 36000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Thanks
you can restrict access to your app directory like so
location /app {
deny all;
return 404; #show not found instead of 403
}
Or you can override the 403 handling by sending a redirect to https://example.com/
location /app{
deny all;
error_page 403 https:/domain.com/; #redirect to your main directory
}

setup vhost with nginx on mamp free version

I'd like to setup vhosts on mamp, while usin nginx instead of the default apache server.
I browsed stack overflow and the web, but all I could find was either about pure nginx (speaking about the sites-available folder which isn't created by default on mamp) or about vhost with apache on mamp or about vhosts with nginx on mamp PRO.
I did manage to switch to nginx, but all I can see now are 403 errors (and when I go back to apache server, all my vhosts are working). I did add the lines in my hosts file, but I can't manage to get the vhosts working. here is my MAMP/conf/nginx/nginx.conf
http {
...
server {
...
location / {
index index.html index.php;
}
location my-vhost-name.com {
root /Users/myName/Document/projectParentFolder/projectFolder;
index index.html index.php;
}
}
}
And when I go to my-vhost-name.com, I have the 403 error message from nginx.
Thank you for your help.
In the MAMP/conf/nginx directory, I created a sites-enabled folder for configs for individual sites.
I added a config file for the site example.com in MAMP/conf/nginx/sites-enabled
I added config vars to MAMP/conf/nginx/sites-enabled/example.com:
server {
charset utf-8;
client_max_body_size 128M;
sendfile off;
listen 80;
server_name example.com;
root /Applications/MAMP/htdocs/example.com/;
index index.php;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass
unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
At the end of the config main file (MAMP/conf/nginx/nginx.conf), I connect all the configs from the sites-enabled folder:
include sites-enabled/*;
}
Restart MAMP

Why would Nginx serve files correctly from the default directory but not new directory?

Installation
OS: CentOS 7.4
Server: Nginx 1.12.2
Question: My nginx installation serves files without problem from the default directory, /usr/share/nginx/html. Unfortunately I get a 403 error when I use a new folder that I created, /www/html. What should I look for?
My permissions are identical for both folders although the owner is different.
Original default folder
drwxr-xr-x. 13 root root 155 Jan 8 09:25 usr
New default folder
drwxr-xr-x. 3 first first 18 Jan 15 10:45 www
I am using the stripped down server.conf file below and it works correctly.
events {}
http {
server {
listen 80;
server_name mydomain.com;
root /usr/share/nginx/html;
}
}
However when I change server.conf to use this directory /www/html I get a 403 error.
Thanks for any help in advance! I'm a noob and not even sure where to look beyond file permissions.
I faced the same problem.
Solution. You must create test.example.com.conf inside the conf.d folder
sudo nano /etc/nginx/conf.d/test.example.com.conf
And the file :
server {
listen 80;
#listen *:80;
server_name test.example.com;
# note that these lines are originally from the "location /" block
root /var/www/test.example.com/;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
restart server systemctl restart nginx.service

Nginx: Setting a default file extension

What rule would I use for nginx so my default file extension is .php?
I currently access a pages using www.mywebsite.com/home.php but I want to just use www.mywebsite.com/home
Thanks
Assuming you also want to serve static files, you could use something like this:
server {
server_name example.com;
# Set the docroot directly in the server
root /var/www;
# Allow index.php or index.html as directory index files
index index.html index.php;
# See if a file or directory was requested first. If not, try the request as a php file.
location / {
try_files $uri $uri/ $uri.php?$args;
}
location ~ \.php$ {
# If the php file doesn't exist, don't pass the request to php, just return a 404
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass your_php_backend_address;
}
}

Resources