@barakuda1

Как правильно настроить проксирование для моего Nextjs?

Здравствуйте. Мучаюсь уже полдня, никак не могу запустить правильно на VPS сервере приложение.
Почему-то неправильно отрабатывает проксирование: при переходе на site.com - не открывается, а если site.com:3001 - отлично открывается приложение.

P.S. адрес сайта / ip поменял.

Конфигурация nginx:

server {
	server_name mysite.com www.mysite.com;
	charset off;
	index index.php index.html;
	disable_symlinks if_not_owner from=$root_path;
	include /etc/nginx/vhosts-includes/*.conf;
	include /etc/nginx/vhosts-resources/mysite.com/*.conf;
	access_log /var/www/httpd-logs/mysite.com.access.log;
	error_log /var/www/httpd-logs/mysite.com.error.log notice;
	ssi on;
	set $root_path /var/www/www-root/data/www/mysite.com;
	root $root_path;
	gzip on;
	gzip_comp_level 5;
	gzip_disable "msie6";
	gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
	location / {
		location ~ [^/]\.ph(p\d*|tml)$ {
			try_files /does_not_exists @php;
		}
		location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf|webp|woff|woff2)$ {
			expires 24h;
		}
		
	    proxy_pass https://localhost:3001;
		proxy_set_header X-Forwarded-Host $host;
		proxy_set_header X-Forwarded-Server $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_set_header Host $http_host;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "Upgrade";
	}
	location /_next/static {
		alias /var/www/www-root/data/www/mysite.com/.next/static;
	}
	location @php {
		include /etc/nginx/vhosts-resources/mysite.com/dynamic/*.conf;
		fastcgi_index index.php;
		fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@mysite.com";
		fastcgi_pass unix:/var/www/php-fpm/1.sock;
		fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
		try_files $uri =404;
		include fastcgi_params;
	}
	return 301 https://$host:443$request_uri;
	listen MY.VPS.IP.ADDRESS:80;
}
server {
	server_name mysite.com www.mysite.com;
	ssl_certificate "/var/www/httpd-cert/www-root/mysite.com_le1.crtca";
	ssl_certificate_key "/var/www/httpd-cert/www-root/mysite.com_le1.key";
	ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
	ssl_prefer_server_ciphers on;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
	ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
	charset off;
	index index.php index.html;
	disable_symlinks if_not_owner from=$root_path;
	include /etc/nginx/vhosts-includes/*.conf;
	include /etc/nginx/vhosts-resources/mysite.com/*.conf;
	access_log /var/www/httpd-logs/mysite.com.access.log;
	error_log /var/www/httpd-logs/mysite.com.error.log notice;
	ssi on;
	set $root_path /var/www/www-root/data/www/mysite.com;
	root $root_path;
	gzip on;
	gzip_comp_level 5;
	gzip_disable "msie6";
	gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
	location / {
		location ~ [^/]\.ph(p\d*|tml)$ {
			try_files /does_not_exists @php;
		}
		location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf|webp|woff|woff2)$ {
			expires 24h;
		}
		location /_next/static {
			alias /var/www/www-root/data/www/mysite.com/.next/static;
		}
	    proxy_pass https://localhost:3001;
		proxy_set_header X-Forwarded-Host $host;
		proxy_set_header X-Forwarded-Server $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_set_header Host $http_host;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "Upgrade";
	}
	location @php {
		include /etc/nginx/vhosts-resources/mysite.com/dynamic/*.conf;
		fastcgi_index index.php;
		fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@mysite.com";
		fastcgi_pass unix:/var/www/php-fpm/1.sock;
		fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
		try_files $uri =404;
		include fastcgi_params;
	}
	listen MY.VPS.IP.ADDRESS:443 ssl http2;
}


То есть, ставлю проксирование с httpS, в server.js также:

const { createServer } = require('https');
const { parse } = require('url');
const next = require('next');
const fs = require('fs');

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = process.env.PORT || 3001;
const httpsOptions = {
  key: fs.readFileSync('/var/www/httpd-cert/www-root/mysite.com_le1.key'),
  cert: fs.readFileSync('/var/www/httpd-cert/www-root/mysite.com_le1.crtca'),
};

const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer(httpsOptions, async (req, res) => {
    try {
      const parsedUrl = parse(req.url, true);
      const { pathname, query } = parsedUrl;

      await handle(req, res, parsedUrl);
     
    } catch (err) {
      console.error('Error occurred handling', req.url, err);
      res.statusCode = 500;
      res.end('internal server error');
    }
  })
    .once('error', (err) => {
      console.error(err);
      process.exit(1);
    })
    .listen(port, () => {
      console.log(`> Ready on https://${hostname}:${port}`);
    });
});


Подскажите, пожалуйста, уже всю голову сломал.
Ошибок в nginx никаких нет.
  • Вопрос задан
  • 251 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы