Nginx 部署 单页面应用 + nodejs api 应用 最佳实践

摘要:
:css|js)${try_files$uri=404;expires1y;access_logoff;add_headerCache-Control“public”;}位置~^.+..+${try_files$uri=404;}位置/{try_files$uri$uri//index.html;}}测试nginx配置。如果nginx t没有问题,请重新启动nginx nginx srelaad,并在浏览器Com中访问xxx。您可以毫无意外地看到前端页面nodeapi服务器。在这里,我使用nestjs演示了如果应用程序需要调用http接口“/api/test”,nestjs中的实现可能如下:@Controller()exportclassAppController{@Gettest(){return{a:1};}现在将本地nestjs程序打包并上传到服务器,然后使用pm2启动它http://xxx.com:3000/api/test您应该看到Nestjs“{a:1}”部署的最佳实践。此时,应该使用nginx反向代理http://xxx.com/api/test达到http://xxx.com:3000/api/test向服务器添加位置,即location/api/{proxy_}passhttp://127.0.0.1:3000 ;} 现在,您的服务器应该看起来像这样的服务器{listen80;server_namexxx.com www.xxx.com;root/var/www/xxx.com/web/;indexindex.html;location/api/{proxy_passhttp://127.0.0.1:3000 ;} 位置~*.(?

Nginx 部署 单页面应用 + nodejs api 应用 最佳实践第1张

假定你的域名是 xxx.com 如果没有域名 则使用你的服务器 ip


现代前端框架打包后基本会将文件放在 /dist 目录下,通常肯定会有一个index.html的文件 /dist/index.html

然后你需要把打包文件上传到服务器,假如你上传到了 "/var/www/xxx.com/web/"

配置 nginx 添加一个 server块 代理静态文件

	server {
		listen 80;
		server_name xxx.com www.xxx.com;
		root	/var/www/xxx.com/web/;
		index index.html;

		location ~* .(?:manifest|appcache|html?|xml|json)$ {
			expires -1;
		}

		location ~* .(?:css|js)$ {
			try_files $uri =404;
			expires 1y;
			access_log off;
			add_header Cache-Control "public";
		}

		location ~ ^.+..+$ {
			try_files $uri =404;
		}

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

测试nginx配置 nginx -t 没有问题就热重启nginx nginx -s reload

然后在浏览器访问 xxx.com 没有意外就能看到前端页面

node api 服务器

我这里使用 nestjs 示范

假如你的应用程序中要调用一个http接口 "/api/test"

那么在 nestjs 中实现可能是这样

@Controller()
export class AppController {
  @Get('/api/test')
  test() {
    return { a: 1 };
  }
}

现在将你本地的nestjs程序 打包上传到服务器,并使用 pm2 启动,假如你的node启动的是 3000 端口,那么访问 http://xxx.com:3000/api/test 应该会看到 "{ a: 1 }"

部署 Nestjs 最佳实践 在这里

这个时候 需要使用nginx反向代理 http://xxx.com/api/testhttp://xxx.com:3000/api/test

在 server 中添加一个location即可

		location /api/ {
			proxy_pass http://127.0.0.1:3000;
		}

现在你的 server 看起来应该是这样

	server {
		listen 80;
		server_name xxx.com www.xxx.com;
		root	/var/www/xxx.com/web/;
		index index.html;

		location /api/ {
			proxy_pass http://127.0.0.1:3000;
		}

		location ~* .(?:manifest|appcache|html?|xml|json)$ {
			expires -1;
		}

		location ~* .(?:css|js)$ {
			try_files $uri =404;
			expires 1y;
			access_log off;
			add_header Cache-Control "public";
		}

		location ~ ^.+..+$ {
			try_files $uri =404;
		}

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

重启 nginx nginx -t && nginx -s reload

现在在网页访问你的web程序,http服务应该能正常工作

现在有了 web客户端和node api,你可能还需要一个 admin 后台 假如的你的后台也是用单页面写的,并且将 base 设置为 "/admin/"

打包你的 admin 项目, 上传到服务器 "/var/www/xxx.com/web/admin/"

配置 nginx "/admin/"

		location /admin/ {
			try_files $uri $uri/ /admin/index.html;
		}

现在你的 server 看起来应该是这样

	server {
		listen 80;
		server_name xxx.com www.xxx.com;
		root	/var/www/xxx.com/web/;
		index index.html;

		location /api/ {
			proxy_pass http://127.0.0.1:3000;
		}


		location ~* .(?:manifest|appcache|html?|xml|json)$ {
			expires -1;
		}

		location ~* .(?:css|js)$ {
			try_files $uri =404;
			expires 1y;
			access_log off;
			add_header Cache-Control "public";
		}

		location ~ ^.+..+$ {
			try_files $uri =404;
		}

		location /admin/ {
			try_files $uri $uri/ /admin/index.html;
		}

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

现在热重启 nginx nginx -t && nginx -s reload

访问 http://xxx.com/ 显示客户端程序

访问 http://xxx.com/admin/ 显示后台程序

提取 server 块

在 "nginx.conf" 同目录下创建 "xxx.com.conf" 然后将server块复制进去,然后在 "nginx.conf" 的http块 中导入 "xxx.com.conf"

现在你的配置可能是这样

[root@96 conf]# cat xxx.com.conf 
server {
	listen 80;
	server_name xxx.com www.xxx.com;
	root	/var/www/xxx.com/web/;
	index index.html;

	location /api/ {
		proxy_pass http://127.0.0.1:3000;
	}

	location ~* .(?:manifest|appcache|html?|xml|json)$ {
		expires -1;
	}

	location ~* .(?:css|js)$ {
		try_files $uri =404;
		expires 1y;
		access_log off;
		add_header Cache-Control "public";
	}

	location ~ ^.+..+$ {
		try_files $uri =404;
	}

	location /admin/ {
		try_files $uri $uri/ /admin/index.html;
	}

	location / {
		try_files $uri $uri/ /index.html;
	}
}
[root@96 conf]# cat nginx.conf
worker_processes  1;

events {
	worker_connections  1024;
}

http {
	include       mime.types;
	default_type  application/octet-stream;
	proxy_cache_path proxycache keys_zone=one:10m;

	sendfile        on;
	keepalive_timeout  65;

	include a.com.conf;
        include b.com.conf;
	include xxx.com.conf; 
}

配置 https

必须要有域名

使用 certbot 配置 https (具体看certbot文档)

你现在的 "xxx.com.conf"

server {
	listen 80;
	listen 443 ssl;
	server_name xxx.com www.xxx.com;
	root	/var/www/xxx.com/web/;
	index index.html;

	ssl_certificate      /etc/letsencrypt/live/xxx.com/cert.pem;
	ssl_certificate_key  /etc/letsencrypt/live/xxx.com/privkey.pem;

	ssl_session_cache    shared:SSL:1m;
	ssl_session_timeout  5m;

	ssl_ciphers  HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;


	location /api/ {
		proxy_pass http://127.0.0.1:3000;
	}


	location ~* .(?:manifest|appcache|html?|xml|json)$ {
		expires -1;
	}

	location ~* .(?:css|js)$ {
		try_files $uri =404;
		expires 1y;
		access_log off;
		add_header Cache-Control "public";
	}

	location ~ ^.+..+$ {
		try_files $uri =404;
	}

	location /admin/ {
		try_files $uri $uri/ /admin/index.html;
	}

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

如果你想使用 docker 配置 nginx + 单页面应用 + nodejs api + 数据库 可以看这里

免责声明:文章转载自《Nginx 部署 单页面应用 + nodejs api 应用 最佳实践》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Mysql基础Spring boot 项目打成war包并在idea中运行下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Nginx错误提示:504 Gateway Time-out解决方法

朋友说504 Gateway Time-out的错误提示与nginx本身是没有任何关系的我们可以通过fastcgi配置参数的调整进行解 决。 修改 php-fpm 配置文件: 1.把 max_children 根据系统修改下,保证有充足的php-cgi进程可以被使用; 修改php-fpm配置文件增加php-cgi进程数量 修改php-cgi进程数: 代码...

Apache/Nginx/IIS 访问日志详解

Apache日志详解 1.Apache日志文件名称及所在路径 日志文件一般都是保存在在apache/logs目录下,实际情况可以根据Apache的配置文件去查找日志文件所在的路径。 例如phpstudy(windows)在 :phpstudy/Extensions/apache/logs; wdcp(linux)在 :www/wdlinux/apache/...

jmeter环境搭建

1.JMeter 介绍      Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序)。它可以用来测试静态和动态资源的性能,例如:静态文件,Java Servlet,CGI Scripts,Java Object,数据库和FTP服务器等等。JMeter可用于模拟大量负载来测试一台服务器,...

通过yum库安装或升级rpms的方法

  使用yum工具升级系统或者安装rpm。  此方法特别适用于如下情况:  1) 升级系统但是没有新版本系统的ISO。  2) 要安装的rpm文件依赖于其他rpm,且依赖关系复杂。 步骤如下:  1. 若升级系统或安装rpm时,有ISO文件,则挂载ISO文件     mount -o loop xxx.iso /mnt     并直接跳到低4步执行。   ...

uboot配置和编译过程详解

uboot主Makefile分析1 1、uboot version确定(Makefile的24-29行) Makefile代码部分: [plain] view plain copy  VERSION = 1   PATCHLEVEL = 30    SUBLEVEL = 4   EXTRAVERSION =    U_BOOT_VERSION =...

模块与包的导入

1.模块什么是模块: #常见的场景:一个模块就是一个包含了python定义和声明的文件(文件名就是模块名字加上.py的后缀),模块可以被导入使用。 #但其实import加载的模块分为四个通用类别:  使用python编写的.py文件 已被编译为共享库或DLL的C或C++扩展 把一系列模块组织到一起的文件夹(注:文件夹下有一个__init__.py文件,该...