0

problem 1 : i trying to access my site under ip local address using http, everytime i access that, it will download php file meaning not execute. problem 2: i trying access my site under my domain with https, but it return result '404 Not Found'

below is my nginx config:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html/nextcloud;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
    rewrite ^ /index.php$uri;
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}

location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php8.1-fpm.sock;
       }
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}


server {

    listen 443;
    server_name _;

root         /var/www/html/nextcloud;

# Add index.php to the list if you are using PHP
#   index index.php index.html index.htm index.nginx-debian.html;

    access_log            /var/log/nginx/jenkins.access.log;

    location / {

      proxy_set_header        Host $host;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_ssl_server_name on;


      #proxy_pass          http://127.0.0.1:8080;
      proxy_read_timeout  90;

      proxy_redirect      http://localhost:8080 https://user.mysite.ml;
    }
location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php8.1-fpm.sock;
       }    
}

can anyone please help with my config?

  • file i host i put at /var/www/html/nextcloud.
  • For php i using php8.1-fpm
  • for nginx i using version nginx/1.18.0
  • My OS is debian
  • The file i hosting is Nextcloud Database Mariadb
1
  • is php8-fpm up and running? create a new basic file with <?php phpinfo(); ?> and see if it's working
    – djdomi
    Jul 24, 2022 at 17:55

1 Answer 1

0

The problem is caused by this part:

location / {
    rewrite ^ /index.php$uri;
    try_files $uri $uri/ ?404;
}

This makes nginx look for /index.php/ when you access the / of the website, and /index.php/test when you access /test of the site.

Your PHP location only matches files that end with .php (\.php$ regex). Therefore the requests are not passed to PHP interpreter.

How to actually fix it depends on how exactly your application wants to receive requests. For example, with Wordpress one just passes all requests to index.php and it handles the rest of URL parsing:

location / {
    try_files $uri $uri/ /index.php;
}

The problem with your HTTPS configuration is that your server_name does not contain your domain name. Therefore nginx uses the default virtual host for the request.

Your HTTPS configuration is also missing ssl_certificate and ssl_certificate_key directives.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .