我正在尝试使用 nginx 将 React 应用程序部署到 ubuntu 服务器。
我开始从主文件夹编译我的源代码
PUBLIC_URL=/var/www/xxx/build npm run build
然后,我将其移至 /var/www/xxx/build
我的 /etc/nginx/sites-available/default
看起来像这样:
server {
listen 80 default_server;
root /var/www/xxx/build;
server_name x.x.x.x; #I am hiding my real IP
index index.html index.htm
location / {
try_files $uri $uri/ =404;
}
location ~ \.css {
alias /var/www/xxx/build/static/css/;
add_header Content-Type text/css;
}
location ~ \.js {
alias /var/www/xxx/build/static/js/;
add_header Content-Type application/x-javascript;
}
}
然后,我更改了访问目录:
chown -R $USER:www-data /var/www/xxx/build
和
chmod 755 -R /var/www/xxx/build
我使所有目录都可以执行:
chmod +x /var/
chmod +x /var/www/
chmod +x /var/www/xxx/
chmod +x /var/www/xxx/build/
最后,我做到了
sudo service nginx restart
我的问题是,当我访问我的域时,禁止通过 /var/www/xxx/build/static/ 访问的资源,为什么?
我被这个问题困了好几个小时,我真的很感谢会尽力帮助解决它的人!!!