0

I set up my domain on my server using nginx. So far so good my homepage works. But now I wanna add some locations for later test of programming. My plan is to call diffrent projects like mydomain.com/php/myprogramm.php

So I add some folder in /var/www/mydomain.com/php (my side index is in /var/www/mydomain.com/html)

Entering www.mydomain.com/php/ leads to an 403 error and mydomain.com/php/myprogramm.php says File not found...

this is my nginx file:

server {
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;

# Make site accessible from http://localhost/
server_name mydomain.com www.mydomain.com;

location / {
    root /var/www/mydomain.com/html;
    index index.html index.htm;
}

location /php/ {
    root /var/www/mydomain.com;
}

location /js/ {
    root /var/www/mydomain.com;
}

location /node/ {
    root /var/www/mydomain.com;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#   # With php5-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

}

Of course when I set up my domain I also set sudo chown -R www-data:www-data /var/www/mydomain.com/html and sudo chmod 755 /var/www

Some ideas someone? :/

ComputerDully
  • 157
  • 1
  • 1
  • 8

2 Answers2

0

Problems analysis

The first golden rule is:

nginx always serves request from a single location only. (Re-)read http://nginx.org/en/docs/http/request_processing.html.

Based on your configuration:

  1. Requests to (www.)mydomain.com/php/<whatever> for files not ending with .php will be served by location /php/ from /var/www/mydomain.com/php/<whatever>
  2. Requests to (www.)mydomain.com/<whatever>.php will be served by location ~\.php$ from <default root ('html' by default)>/<whatever>.php

The first problem here is that you are not serving .php files from where you think you are. Learn from location documentation how the location block serving a request is chosen.

You will note that the 'File not found' error was not an nginx error, but a message generated by PHP. That helps to know whether the problem comes from (frontend or backend).

Now about that 403: it seems nginx has trouble accessing the location where it is supposed to serve content from. Check /var/www/mydomain.com/php/ (directory + contents) rights.

Proposed pieces of advice

Your configuration looks suboptimal.

  1. If you use the same root in lots of location blocks, why not moving it one level upper so it becomes the default (which yo ucan override in specific locations where needed)?
  2. You can used nested locations, ie to solve you PHP files serving problem. Note that it is always a good idea to enclose regex locations inside prefix locations (What is the difference? Read location documentation). The reason is regex locations are order-sensitive, which is bad for maintenance. Prefix locations are not since only the longest match with a request URI will be chosen.

Here is a propsed updated version of part of your configuration:

root /var/www/mydomain.com;

location / {
    root /var/www/mydomain.com/html;
    index index.html index.htm;
}

location /php/ {
    location ~ \.php$ {
        # Useless without use of $fastcgi_script_name and $fastcgi_path_info
        # Moreover, requests ending up here always end with .php...
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;

        # You seem to have copy-pasted this section without understanding it.
        # Good understanding of what happens here is mandatory for security.
    }
}

I suggest you read the documentation about fastcgi_split_path_info, $fastcgi_script_name and $fastcgi_path_info.

Bernard Rosset
  • 4,523
  • 6
  • 27
  • 29
0

For my testing right now I fixed the issue quite simply.

  1. I forogt to check my php.ini and change the cgi.fix_pathinfo to 0
  2. Also I changed the group for my folders (still had root inside) to www-data.
  3. At the end I updated my configuration: I set root /var/www/mydomain.com; in my server block (server{})

That's all I did.

But I will keep your advice in mind for later issues.

Thanks for your help I appreciate it.

ComputerDully
  • 157
  • 1
  • 1
  • 8