服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - nginx配置多个前端项目实现步骤

nginx配置多个前端项目实现步骤

2023-03-07 17:03清风笑~ Nginx

本文主要介绍了nginx配置多个前端项目实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

最近一台服务器要配置多个前端项目,当然前后端分离就需要nginx来配置了。

单个项目还好说,如下
修改nginx的nginx.conf配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
pid /usr/local/nginx/logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
 
    server {
        listen       8000;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
        
        location / {
            root   /var/www/;
            #index  index.html index.htm;
        }
        location ~ /static/.*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            root /var/www/project;
        }
 
        location ~ /static/.*\.(js|css)$ {
            root /var/www/project;
        }
 
        location = /project {
            root   /var/www/project;
            index  index.html index.htm;
        }
   
    }
 
}

但是出现了多个项目也需要在nginx.conf配置

项目基于vue cli 开发的,打包时需要配置一下js,css 等静态文件的连接地址
修改如下配置文件

nginx配置多个前端项目实现步骤

根据项目名字或者路径名 修改 在对应的项目里

?
1
2
3
assetsPublicPath: '/project/'
-----------------------
assetsPublicPath: '/project1/'

然后再来配置nginx.conf

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
user root;
worker_processes  1;
 
pid /usr/local/nginx/logs/nginx.pid;
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    #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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    server {
        listen       8000;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
        
        location / {
            root   /var/www;
            #index  index.html index.htm;
        }
 
        location = /project1 {
            root   /var/www/project1;
            try_files $uri $uri/ /project1/index.html;
            index  index.html index.htm;
        }
        
        location = /project2{
            root /var/www/project2;
            try_files $uri $uri/ /project2/index.html;
            index  index.html index.htm;
        }
 
    }
 
}

此处注意呢 user root; 需要加上, 不然范围报 500,
然后重启一下nginx

?
1
2
3
4
先停止
 ./nginx -s quit
再重启
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

当然nginx -s reload 可以 ,但是可能报错, 解决就用上面办法

nginx配置多个前端项目实现步骤

成功访问
192.168..:8000/project/index.html
192.168..:8000/project1/index.html

到此这篇关于nginx配置多个前端项目实现步骤的文章就介绍到这了,更多相关nginx配置多前端项目内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/ling369523246/article/details/82462307

延伸 · 阅读

精彩推荐
  • NginxNginx缓存设置案例详解

    Nginx缓存设置案例详解

    这篇文章主要介绍了Nginx缓存设置案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    多纤果冻7022021-09-27
  • NginxNginx反向代理学习实例教程

    Nginx反向代理学习实例教程

    nginx作为web服务器一个重要的功能就是反向代理,当然你也可以使用nginx配置正向代理,这篇文章主要给大家介绍了关于Nginx反向代理的相关资料,需要的朋友可...

    Leida_hzm4142021-11-09
  • Nginxnginx rewrite 伪静态配置参数和使用例子

    nginx rewrite 伪静态配置参数和使用例子

    nginx下伪静态配置参数详细说明,使用nginx的朋友,nginx rewrite 伪静态配置参数和使用例子 附正则使用说明 ...

    服务器之家2552019-10-08
  • Nginx使用Nginx搭建高可用高并发的Wcf集群

    使用Nginx搭建高可用高并发的Wcf集群

    很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk对 C# 不大友好,实现起来相对来说比较麻烦,实际情况下,如果你的负载...

    一线码农聊技术2442020-12-03
  • Nginxkeepalived对nginx进行高可用搭建及原理详解

    keepalived对nginx进行高可用搭建及原理详解

    这篇文章主要为大家介绍了keepalived对nginx进行高可用搭建及原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    乔克7292022-09-02
  • NginxNginx 部署的虚拟主机使用 Let's Encrypt 加密 https的方法

    Nginx 部署的虚拟主机使用 Let's Encrypt 加密 https的方法

    HTTPS 是现在网站的标配了,很多服务都是必须使用 https,如果你不使用的话,浏览器可能就不会对你非常友好了,这篇文章主要介绍了Nginx 部署的虚拟主机...

    huyuchengus9312022-07-09
  • NginxNginx的location的常见规则优先级问题

    Nginx的location的常见规则优先级问题

    Nginx是反向代理和负载均衡的首选工具,nginx的location配置有许多细节内容在网上不容易找到资料,或者解释不清。本文对Nginx location规则优先级介绍,需要...

    阿蔡BLOG6012021-09-23
  • NginxNginx根据不同浏览器语言配置页面跳转的方法

    Nginx根据不同浏览器语言配置页面跳转的方法

    这篇文章主要介绍了Nginx根据不同浏览器语言配置页面跳转的方法,包括一个简体繁体的基本判断方法及实际根据中英文跳转的例子,需要的朋友可以参考下...

    raysong8192019-11-12