上传web代码到nginx

1,535 阅读2分钟

1.链接远程服务器

命令 ssh 10.10.89.22 -l oracle

意思就是远程登录 10.10.89.22 IP 上的 oracle 用户(会提示输入密码)

列子: ssh 10.96.76.98 -l root

2.打开配置文件

命令 cd /etc/nginx

命令 vim nginx.conf

3.配置文件(简单配置)

server {
        # 启动后的端口
        listen       8880;   
        
        # 启动时的地址
        server_name  localhost;

        # 启动后,地址栏输入: localhost:8880, 默认会在html文件夹下找 index.html文件
        location / {
            root   html;
            index  index.html; 
        }
        
        # 404页面配置,页面同样在html文件夹中
        error_page  404    /404.html;
        location = /404.html {
            root   html;
        }
        

        # 其他错误码页面配置
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


        # 配置代理。由于项目是在本地起动的,而我们的request需要请求其他ip地址。如果你的request链接为localhost:8880/api/login?name=12345,那么下面配的就是location /api,最终会代理到 http://192.168.0.0:80/api/login?name=12345
        
        location /api {
           proxy_pass   http://192.168.0.0:80;       
        }

        # 一把前端不管用vue,还是react等框架,默认都是单页面的,如果你的项目是多页面的,则需要用到下面的配置。
        # 因为此时你的浏览器的url不是localhost:8880/#/login,而是 localhost:8880/a.html/#/login
        # 所以我们需要将路径中a.html指向具体的html文件夹中的文件,因为默认是index.html
        location /a.html {
          alias html/xxx/xxx; #文件路径
          index a.html; #路径下的指定文件
        }
        location /b.html{
          alias html;
          index b.html;
        }
        # 最终通配符解决多页面方案
         location ^~ /cmb_data_market/ {
               alias /usr/share/nginx/html/cmb_data_market/;
         }
         # ^~ /cmb_data_market/ 意思以 /cmb_data_market/ 不间断连字符的url
         #在这个location配置段中,如果URL请求“/cmb_data_market/login.html#
         /”,那么nginx将会在服务器上查找“/usr/share/nginx/html/cmb_data_mar
         ket/login.html”文件,即请求的URL中location后面的部分会被追加到ali
         as指定的目录后面,而location后面的“/cmb_data_market/”路径将会被自
         动丢弃。
}

4.打包文件(npm run build)

将打包好的文件根据项目需求重命名。

切换远程的上传目录

命令 cd /usr/share/nginx/html

删除以前的文件(如果有)列子: 删除 cmb_data_market 文件 rm -rf cmb_data_market

5.上传文件

在要上传文件目录上终端打开将文件上传至远程服务器。

命令 scp -r cmb_data_market/ root@10.96.76.98:/usr/share/nginx/html/

cmb_data_market/要上传的文件,/usr/share/nginx/html/上传到哪里

6.重启 nginx -s reload

7.总结

个人摸索,还需要多多努力。