0

I'm really out of luck today. I don't know what it is, but I can't get NGINX to do, what it should do.

I want to achieve the following:

domain.com shows index.html (/var/www/domain/index.html)

domain.com/upload (also domain.com/upload/) shows upload.php (/var/www/domain/upload.php)

Serving only the index.html isn't a problem. However, I have problems with the /upload part.

I tried it using location /upload and location = /upload but didn't really have luck. One time tho, I got it working but ditched it right away because the php file was downloading and not serving.

If you could tell me, what exactly my server block should look like, I'd be really thankful.

lxxrxtz
  • 1
  • 1

1 Answers1

0
server {

  listen   80; ## listen for ipv4
  listen   [::]:80 default ipv6only=on; ## listen for ipv6

  server_name  www.example.com;

  access_log  /var/log/nginx/example.com.access.log;
  error_log   /var/log/nginx/example.com.error.log debug;

  root /var/www/domain/;
  index index.php index.html;

  location /static {
    try_files $uri $uri/ @php_index =404;
  }

  location / {
    try_files $uri $uri/ $uri/index.html $uri/index.htm @php;
  }

  location @php {
    try_files $uri /index.php =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass unix:/tmp/php-cgi/php-cgi.socket;
   fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
  }

  location ~ \.php$ {
    try_files $uri /index.php =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass unix:/tmp/php-cgi/php-cgi.socket;
    fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
  }
}
BradChesney79
  • 67
  • 5
  • 12
  • I've tried it and changed it, so index.php would get upload.php (keep in mind that it's a different page than index.html) and changed the root directory of course. Now the index.html works normally but trying to request domain.com/upload I get 502. Could this be that you left out the part specifying the /upload domain-thingy? – lxxrxtz Apr 29 '21 at 19:07
  • Create a directory called upload, /var/www/domain/upload. Put the php script in there. Are you using a framework or web application? – BradChesney79 Apr 29 '21 at 19:09
  • Web Application, but I've now created that directory, put the file into there but get a 403. What am I missing now? – lxxrxtz Apr 29 '21 at 19:17
  • What is the web application? Are the files owned by www-data? ( ls -la /var/www/domain/upload/ ) If you look at the single dot, at the top, you can see the ownership of the current directory-- then your script will also be listed. Take a look at the file permissions also. They should be at least read for owner (r-- --- --- or 400) directories need the execute bit set to make them traversable (r-x --- --- or 500). The three sections specify owner group and everybody. I suspect nginx/php cannot access the file. – BradChesney79 Apr 29 '21 at 19:40
  • I am assuming Debian or a derivative like Ubuntu... also. www-data is the user the web server usually runs as. For what it is worth. – BradChesney79 Apr 29 '21 at 19:42