linux下Nginx部署前后端项目

发布于:2023-01-09 ⋅ 阅读:(740) ⋅ 点赞:(0)

Nginx 常用命令(Linux)

cd/usr/local/nginx/sbin
./nginx //启动
./nginx -s stop //停止
./nginx -s quit //安全退出
./nginx -s reload //重新加载配置文件
./ps aux|grep nginx //查看nginx进程

nginx的http配置


    server {
        listen       8888;
        server_name  localhost;
        
        location / {
            root   html;
            index  index.html index.htm;
        }

        location /mobile {
                alias /usr/local/nginx/html/mobile;
                index index.html;
        }
        
        #nginx部署vue项目出现404查找不到js,css文件解决方法
         location ~* \.(gif|jpg|jpeg|png|css|js|ico|css|eot|svg|ttf|woff|mov)$ {
            root /usr/local/nginx/html/mobile;
            expires   48h;
            access_log   off;
         }

         location /pc {
                alias /usr/local/nginx/html/pc;
                index index.html;
         }

        # apk下载
        location /apk{
            alias /usr/local/src/apk/;
            autoindex on;
            default_type application/octet-stream;
        }

        # 浏览器图片预览
        location /images{
            alias /usr/local/src/img/;
            autoindex on;
        }

        # 前后端不分离项目 jar包项目端口转发
        location /visitor/{
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-Ip $remote_addr; # 允许IP访问
            proxy_set_header X-Forwarded-For $remote_addr;
            # 反向代理
            proxy_pass http://127.0.0.1:8083/;
        }

        # 前后端分离项目 部署
        location /cdsm/{
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://localhost:8082/;
        }
   }

nginx 部署多个前端项目

一 : 首先 , 在服务器/usr/local/nginx/html中新建前端项目文件夹(这里取名pc和mobile),用于存放vue打的包.
在这里插入图片描述
二 : vue.config.js文件修改publicPath 属性 , 使index.html文件link到css文件时的路径如下图.

module.exports = {
    publicPath:'/pc/',
    ...
}

在这里插入图片描述
三 : 将vue项目打好的包 分别上传到 对象的项目文件夹下(pc和mobile)
四 : 配置nginx的前端代理

        listen       8888;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        location /pc/{
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html; # 这个是方式前端页面刷新404
        }
        
        location /mobile/{
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html; # 这个是方式前端页面刷新404
        }

五 : 将java项目jar 包长传到服务器 , 并且运行.

 nohup java -jar ***.jar &

网站公告

今日签到

点亮在社区的每一天
去签到