1

Im trying to have nuxtjs as front-end and laravel as backend with NGINX

After adding ssl and reconfiguring the nginx .conf file , Now my error_page line is not working and it does not go to location @php but instead it throws a front-end 404 ( nuxt error page)

server{
listen 45.xx.xxx.xxx:80;
server_name example.com www.example.com;
 root /home/example/core/public/;

return 301 https://www.example.com$request_uri;
}


server {
    listen 45.xx.xxx.xxx:443 ssl;
    # NOTE: SSL configuration is defined elsewhere
     server_name example.com;
   ssl_certificate /etc/pki/tls/certs/example.com.cert;
    ssl_certificate_key /etc/pki/tls/private/example.com.key;
    return 301 https://www.example.com$request_uri;

}


server {
    listen 45.xx.xxx.xxx:443 ssl;
    server_name  www.example.com;
    ssl_certificate /etc/pki/tls/certs/example.com.cert;
    ssl_certificate_key /etc/pki/tls/private/example.com.key;
      root /home/rabter/core/public/;
        index index.php;
        access_log /var/log/nginx/example.com.bytes bytes;
       access_log /var/log/nginx/example.com.log combined;
      error_log /var/log/nginx/example.com.error.log error;
   ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location / {
  root /home/example/core/public/;
        index index.php;
    proxy_set_header                Connection "keep-alive";
    proxy_set_header                Upgrade $http_upgrade;
    proxy_set_header                Connection 'upgrade';
    proxy_http_version              1.1;
    proxy_pass                      https://45.xx.xxx.xxx:3000$request_uri;
    error_page                      404 = @php;
}

location @php {
    try_files                   $uri $uri/  /index.php?$query_string;
}

location ~ \.php$ {
    fastcgi_split_path_info         ^(.+\.php)(/.+)$;
    fastcgi_param HTTPS $https;
    fastcgi_pass                    45.xx.xxx.xxx:9000;
    fastcgi_index                   index.php;
    include                         fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors        off;
    fastcgi_buffer_size             16k;
    fastcgi_buffers                 4 16k;
    fastcgi_connect_timeout         300;
    fastcgi_send_timeout            300;
    fastcgi_read_timeout            300;
}
}

Before adding the ssl , the error_page 404 = @php was working without problem and api calls where made.it looks like nginx ignoring the error_page line. Thanks for any help

Pc Monk
  • 31
  • 7
  • I guess you need `proxy_intercept_error on` directive. But better solution is to have separate “directory” for requests that should be processed by PHP. – Alexey Ten Jun 10 '20 at 06:33

1 Answers1

0

the right configuration file for ssl

The http Configuration File

server {
  listen your-server-ip:80;
    server_name example.com;
        return 301 https://www.example.com$request_uri;

}

The ssl configuration file

server {
  listen your-server-ip:443 ssl;
    server_name example.com;
        return 301 https://www.example.com$request_uri;

}
server {
    listen your-server-ip:443 ssl;
    server_name www.example.com;
    ssl_certificate /etc/pki/tls/certs/example.com.bundle;
    ssl_certificate_key /etc/pki/tls/private/example.com.key;
      root /home/example/core/public/;
        index index.php;
        access_log /var/log/nginx/example.com.bytes bytes;
       access_log /var/log/nginx/example.com.log combined;
      error_log /var/log/nginx/example.com.error.log error;

location / {

    proxy_set_header                Connection "keep-alive";
    proxy_set_header                Upgrade $http_upgrade;
    proxy_set_header                Connection 'upgrade';
    proxy_http_version              1.1;
    proxy_pass                      https://your-server-ip:3000$uri;
    proxy_intercept_errors          on;# In order to use error_page directive this needs to be on
    error_page                      404 = @php;
}

location @php {
    try_files                       $uri $uri/  /index.php?$query_string;

}

location ~ \.php$ {
   fastcgi_split_path_info         ^(.+\.php)(/.+)$;
    fastcgi_pass                    your-server-ip:9000;
    fastcgi_index                   index.php;
    include                         fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_intercept_errors        off;
    fastcgi_buffer_size             16k;
    fastcgi_buffers                 4 16k;
    fastcgi_connect_timeout         300;
   fastcgi_send_timeout            300;
    fastcgi_read_timeout            300;
}
}

Keep in mind my port was 3000 yours might be different

fastcgi_pass can be pointing to a sock too but I directly added it

fastcgi_pass port is set to 9000 in my case Yours can be different

with those being said , We are redirecting 80 and 433 without www to www and then doing a reverse proxy , if the reverse proxy is 404 we try @php and at bottom we are using php-fpm to run our php code

I almost spent 2 weeks learning nginx and writing different configuration till I cameup with this one

Pc Monk
  • 31
  • 7