nginx 404 Not Found Not Found

vqlkdk9b  于 2023-11-17  发布在  Nginx
关注(0)|答案(7)|浏览(178)

下面是我的nginx配置文件。
在默认的.conf中,第一个位置用于访问/usr/share/nginx/html目录,当我访问http://47.91.152.99时可以。但是当我为目录/usr/share/nginx/public目录添加新位置时,nginx在我访问http://47.91.152.99/test时返回404页面。
那么,到底是怎么回事?我是不是滥用了nginx的指令?

  1. /etc/nginx/nginx.conf
  2. user nginx;
  3. worker_processes 1;
  4. error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include /etc/nginx/mime.types;
  11. default_type application/octet-stream;
  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';
  15. access_log /var/log/nginx/access.log main;
  16. sendfile on;
  17. #tcp_nopush on;
  18. keepalive_timeout 65;
  19. #gzip on;
  20. include /etc/nginx/conf.d/*.conf;
  21. }
  22. /etc/nginx/conf.d/default.conf
  23. server {
  24. listen 80;
  25. server_name localhost;
  26. #charset koi8-r;
  27. #access_log /var/log/nginx/log/host.access.log main;
  28. location / {
  29. root /usr/share/nginx/html;
  30. index index.html index.htm;
  31. }
  32. location ^~ /test/ {
  33. root /usr/share/nginx/public;
  34. index index.html index.htm;
  35. }
  36. #error_page 404 /404.html;
  37. # redirect server error pages to the static page /50x.html
  38. #
  39. error_page 500 502 503 504 /50x.html;
  40. location = /50x.html {
  41. root /usr/share/nginx/html;
  42. }
  43. }

字符串

raogr8fs

raogr8fs1#

您似乎在下面的代码块中误用了root指令;

  1. location ^~ /test/ {
  2. root /usr/share/nginx/public;
  3. index index.html index.htm;
  4. }

字符串
前面的代码块告诉nginx在/usr/share/nginx/public文件夹中查找test目录。如果该位置没有可访问的test文件夹,nginx将返回404。为了解决这个问题,我建议使用alias而不是root指令,如下所示;

  1. location ^~ /test/ {
  2. alias /usr/share/nginx/public;
  3. index index.html index.htm;
  4. }


在这个块中,使用alias而不是root指令,对domain.com/test/somefile.jpg的请求将在/usr/share/nginx/public中查找文件somefile.jpg
有关alias指令的更多信息,请访问nginx.org
另外,只是为了好玩,index指令可以设置为一般,所以你不必重写它所有的时间,像这样;

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. root /usr/share/nginx/html;
  5. index index.html index.htm;
  6. error_page 500 502 503 504 /50x.html;
  7. location / { }
  8. location ~^/test/ {
  9. alias /usr/share/nginx/public;
  10. }
  11. location = /50x.html {
  12. root /usr/share/nginx/html;
  13. }
  14. }


有一件事你也应该考虑..
希望对你有帮助。

展开查看全部
pcrecxhr

pcrecxhr2#

根指令出错

  1. location ^~ /test/ {
  2. root /usr/share/nginx/public;
  3. index index.html index.htm;
  4. }

字符串

使用alias指令修复

  1. location ^~ /test/ {
  2. alias /usr/share/nginx/public;
  3. index index.html index.htm;
  4. }

其他改进

额外提示:可以设置index指令,这样你就不必重写它。

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. root /usr/share/nginx/html;
  5. index index.html index.htm;
  6. error_page 500 502 503 504 /50x.html;
  7. location / { }
  8. location ~^/test/ {
  9. alias /usr/share/nginx/public;
  10. }
  11. location = /50x.html {
  12. root /usr/share/nginx/html;
  13. }
  14. }


nginx部分基于配置中的位置来匹配Location块。理想情况下,您可以颠倒现在的位置。在nginx配置中,Location块会更高。为此,location = /50x.html也会向上移动。顺序是
1.完全匹配=
1.正向匹配^~ /
1.大小写敏感正则表达式~ /
1.不区分大小写的regex ~*
1.路径匹配/
关于nginx location priority的更多信息。此外,您可以随时查看官方文档。位置块http://nginx.org/en/docs/http/ngx_http_core_module.html#location的nginx文档

展开查看全部
brvekthn

brvekthn3#

当你的app是vuejs时,你需要这样写,可以防止404,注意双重/测试/

  1. location ^~/test/ {
  2. alias /usr/local/soft/vuejs/;
  3. try_files $uri $uri/ /test/index.html;
  4. }

字符串

2jcobegt

2jcobegt4#

我刚刚解决了这个问题(index.html找不到)。
对我来说,我错误地键入了我的项目名称,以匹配您的ec2项目名称与nginx路径。
移动到nginx/sites-enabled检查nginx路径

  1. cd /etc/nginx/sites-enabled
  2. cat您的项目名称
    1.检查你的nginx路径(对我来说:root/home/ubuntu/practice/current/public;)
    移动到主目录以检查项目名称
  3. CD
  4. LS
    6.如果你的ec2项目名(对我来说:practice)与你的nginx路径名(对我来说:practice)不匹配,那么你可能会得到“index.html not found error”。
mum43rcc

mum43rcc5#

我的server-blocks.conf

  1. server {
  2. listen 80;
  3. index index.html index.htm index.nginx-debian.html;
  4. server_name 13.xxx.xxx.xx;
  5. location / {
  6. root /var/www/portaladmin/;
  7. proxy_pass http://13.xxx.xxx.xx:80/;
  8. }
  9. error_log /var/log/nginx/portaladmin-erorr.log;
  10. }

字符串
我的load-balancer.conf

  1. server {
  2. listen 80;
  3. server_name xx.xxx.xxx.xx
  4. access_log /home/ec2-user/logs/lb-access.log;
  5. error_log /home/ec2-user/logs/lb-error.log;
  6. location / {
  7. proxy_set_header Host $host;
  8. proxy_set_header X-Real-IP $remote_addr;
  9. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  10. proxy_pass http://13.xxx.xxx.xx:80;
  11. }
  12. }

展开查看全部
vwoqyblh

vwoqyblh6#

使用Kubernetes创建nginx&sftppod..我发现我的sftp-deployment.yaml文件中的mountPath与我的sftp-server中的用户名相关。
当我更改了username而没有更改mountPath值以匹配我的用户名时,错误就发生了。所以,文件被上传到username '/home/old-username'而不是username '/home/new-username'

7tofc5zh

7tofc5zh7#

cd /etc/nginx/sites-available/ sudo nano default
在https或服务器中的此配置文件中
$url /$url/index. html

相关问题