刷新React应用程序,在Ubuntu上获取NGINX 404错误

发布于 2025-02-11 14:40:05 字数 573 浏览 0 评论 0原文

好的,所以每次我在不是根/主页上的页面上刷新我的React应用程序时,我会收到404错误。我已经做了很多挖掘,发现我应该更改我的nginx配置文件。唯一的问题是对其他所有人都有用的变化对我来说绝对没有做任何事情。

文件路径 - /etc/nginx/stites -abailable/default

我的nginx配置文件看起来像这样 -

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri /index.html;
        }

我什至尝试在/var/www/html上添加root目录;毫不奇怪,它行不通。

基于我所看到的一切都应该起作用...那为什么不呢?这现在是React路由器问题吗?任何帮助都将非常感谢,我对这个问题感到不安。如果需要更多信息,我可以很乐意提供它,我敢肯定,不确定还有什么原因是该问题的原因。

Alright so every time I refresh my React app on a page that isn't the root/home page I get a 404 error. I've already done a lot of digging and found that I'm supposed to change my nginx config file. The only issue is this change that's working for everyone else is doing absolutely nothing for me.

File path - /etc/nginx/sites-available/default

My nginx config file looks like this -

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri /index.html;
        }

I've even tried adding a root directory at /var/www/html; To no surprise it didn't work.

Based off everything I've seen this SHOULD work... so why isn't it? Is this now a React Router issue or something? Any help would be greatly appreciate, I'm beyond stumped with this issue. If more information is needed I can gladly provide it, I'm sure not sure what else could be the cause of this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2025-02-18 14:40:05

经过数小时与nginx.config弄乱后,我终于解决了我的问题。

目录 - /etc/nginx/sites -abailable |然后 - sudo nano默认

对我有用的东西 -

# Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ $uri.html /index.html;
        }

我的根仍设置为 - root/var/www/html,

对于任何遇到麻烦的人都可以完全清除所有Nginx文件并重新安装清洁的板岩。我认为我的错误是我添加了“ nginx.conf”文件,这最终导致了其他配置文件“默认”。因此,总而言之,仅触摸默认配置文件,然后将nginx.conf作为默认值。

After hours of messing around with the nginx.config I finally solved my issue.

Directory - /etc/nginx/sites-available | then - sudo nano default

What worked for me -

# Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ $uri.html /index.html;
        }

My root is still set to - root /var/www/html

For anyone having troubles try completely purging all nginx files and reinstalling for a clean slate. What I think was wrong for mine was I had added to the "nginx.conf" file and that ended up causing complications with the other config file "default." So all in all, only touch the default config file, and leave nginx.conf as default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文