title: How to deploy Nextjs/Laravel project with nginx?
body: <p>I am using Laravel-8 framework as Restful API server, and Nextjs as client render. I created two folders:</p>
  <p>/var/www/domain/backend (Laravel 8 app)</p>
  <p>/var/www/domain/frontend (Nextjs app)</p>
  <p>To run nextjs app I am using pm2. (npm run build then pm2 start). I have two nginx configuration files:</p>
  <p>/etc/nginx/conf.d/frontend.conf</p>
  <p>/etc/nginx/conf.d/backend.conf</p>
  <p>Before production frontend run in port 3000 and backend run in port 8000.
  The application not working properly, it gives some errors below:</p>
  <pre><code>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/api/index. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
  
  Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/api/index. (Reason: CORS request did not succeed)
  
  Error in error page `getInitialProps`:  TypeError: t.response is undefined
  </code></pre>
  <p>How can I correctly deploy laravel and nextjs app? Is there any best way to deploy them?</p>
  <p>frontend.conf:</p>
  <pre><code>upstream frontend_upstream {
      server localhost:3000;
  }
  
  server {
      listen 80 default_server;
  
      server_name _;
  
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
  
      location /_next/static {
          proxy_cache STATIC;
          proxy_pass http://frontend_upstream;
      }
  
      location /static {
          proxy_cache STATIC;
          proxy_ignore_headers Cache-Control;
          proxy_cache_valid 60m;
          proxy_pass http://frontend_upstream;
      }
  
      location / {
          proxy_pass http://frontend_upstream;
      }
  }
  </code></pre>
  <p>backend.conf</p>
  <pre><code>server {
  
      listen         80;
  
      server_name  domain.com www.domain.com;
  
      root         /var/www/domain/backend/public;
      index    index.php index.html
  
      add_header X-Frame-Options &quot;SAMEORIGIN&quot;;
      add_header X-XSS-Protection &quot;1; mode=block&quot;;
      add_header X-Content-Type-Options &quot;nosniff&quot;;
  
      charset utf-8;
  
      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }
  
      error_page 404 /index.php;
      
      location ~ \.php$ {
          fastcgi_pass unix:/run/php/php8.0-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
          include fastcgi_params;
          fastcgi_read_timeout 300;
      }
          
      location ~ /\.ht {
          deny all;
      }
  }
  </code></pre>