1

I have nginx as a webserver with PHP-fpm. I can successfully see my website using http but if I use https I will download the index page and the page will not be shown.

My nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    server_tokens off;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    fastcgi_read_timeout 300s;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    client_max_body_size 2M;

    include /etc/nginx/conf.d/*.conf;
}

    server {
        listen 443 ssl;
        server_name  www.domain.net;
        root         /usr/share/nginx/html;


        add_header X-Frame-Options "SAMEORIGIN";
        ssl_certificate /etc/letsencrypt/live/www.domain.net/fullchain.pem; 
        ssl_certificate_key /etc/letsencrypt/live/www.domain.net/privkey.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
        ssl_prefer_server_ciphers on;

        location / {
            index index.php;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

And conf.d/default.conf

server {
listen   80;
server_name  www.domain.net;

root   /usr/share/nginx/html;
index index.php index.html index.htm;

location / {
    try_files $uri $uri/ =404;
}
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_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;
}
}
executable
  • 3,365
  • 6
  • 24
  • 52

1 Answers1

3

Your php location tag only exists in the listen 80 server block. So if the request is sent to the 443 server block, it doesn't handle any php files. You should add the same php block to the 443 server block;

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    server_tokens off;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    fastcgi_read_timeout 300s;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    client_max_body_size 2M;

    include /etc/nginx/conf.d/*.conf;
}

    server {
        listen 443 ssl;
        server_name  www.domain.net;
        root         /usr/share/nginx/html;


        add_header X-Frame-Options "SAMEORIGIN";
        ssl_certificate /etc/letsencrypt/live/www.domain.net/fullchain.pem; 
        ssl_certificate_key /etc/letsencrypt/live/www.domain.net/privkey.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
        ssl_prefer_server_ciphers on;

        location / {
            index index.php;
        }

        # NEW
        location ~ \.php$ {
            try_files $uri =404;
            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;
          }
        ## NEW

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Do you see any errors in the nginx log files? If so; please add them to your original post – 0stone0 Feb 28 '19 at 15:47
  • 1
    I made it working, I needed to add `index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } ` – executable Feb 28 '19 at 15:49