react 部署在ngnix上(windows环境)

目录

1.下载nginx。

2.测试nginx

3.配置react项目

4.nginx常用命令


1.下载nginx。

2.测试nginx

解压后,可以在cmd命令行输入:start nginx,开启服务,并在浏览器输入localhost,如果成功打开,则说明该nginx可以使用了。

出现该效果说明运行成功 

3.配置react项目

我的项目打包后目录结构:

现在我们开始配置react项目

打开nginx.conf文件

在http里面添加一个server对象

server {
        listen       8088;
        server_name  localhost;

		root E:/items/react/rec-webp4/dist;
        index index.html;

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

        location @router {
        rewrite ^.*$ /index.html last;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

listen  :设置的端口号

server_name  :访问的名字

root   :你项目所放的地址

index index.html  :你的入口html文件

因为是单页应用,所以,是根据路由跳转,所以,为了避免出现404,我们需要重写至index.html

     #根据路由设置,避免出现404
        location / {

             try_files $uri $uri/ @router;
             index index.html;
         }

        location @router {
            rewrite ^.*$ /index.html last;
        }

设置代理:

nginx可以设置反向代理,解决跨域问题,我们可以这样设置:

# 匹配 api 路由的反向代理到API服务
location /api/ {
     proxy_pass http://192.168.xxx.xxx:xxxx/;
}

这样,就会由:

http://localhost:10003/api/user/getuser...

转发至:

http://192.168.xxx.xxx:xxxx/user/getuser...

好了,现在,你就可以重启服务查看效果,(因为之前使用了start nginx ,所以这里可以直接reload重启)

nginx -s reload

4.nginx常用命令

nginx开启命令:start nginx

nginx停止命令:nginx -s quit

nginx重启命令:nginx -s reload

完~

 

 

 

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值