【问题标题】:Why does nginx think my root directory is /usr/share/nginx and not /var/www/html as my configuration states?为什么 nginx 认为我的根目录是 /usr/share/nginx 而不是 /var/www/html 作为我的配置状态?
【发布时间】:2021-06-10 03:06:14
【问题描述】:

我是新的 nginx 并试图掌握它。我一直在阅读文档,他们说如果使用指令 root 它应该告诉 nginx 在哪里可以找到请求。例如,根据我的理解,root /var/www/html 应该告诉 nginx 在目录/var/www/html 中查找请求,但我的 nginx 实例没有这样做。我试图在该目录中加载一个名为test.html 的文件,但它试图在/usr/share/nginx 中查找该文件。请注意,这是 nginx 的全新安装,我对默认配置文件进行了一些更改。我还想注意路径前缀设置为/usr/share/nginx,但我的理解是使用root 指令应该覆盖它。我正在运行 Ubuntu 18.04 并通过 apt 安装 nginx。如果您需要更多信息,请告诉我。谢谢!

nginx.conf - 请注意此文件没有未注释的根指令

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##


    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
    }

    server {
      listen 80;

      server_name kramericaindustries.hopto.org;
      rewrite ^/rstudio$ $scheme://$http_host/rstudio/ permanent;

      location /rstudio/ {
        rewrite ^/rstudio/(.*)$ /$1 break;
        proxy_pass http://localhost:8787;
        proxy_redirect http://localhost:8787/ $scheme://$http_host/rstudio/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 20d;
      }

      location /heatmap/ {
        proxy_pass http://127.0.0.1:8050;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

      }

      # location /test/ {
      #   root /home/grant/test;
      #   index index.html;
      # }
    }

    # server {
    #   listen 8050;

    #   server_name kramericaindustries.hopto.org;

    #   location /heatmap/ {
    #     proxy_pass http://127.0.0.1:8050;
    #     proxy_set_header Host $host;
    #     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #   }
    #   location /test/ {
    #   }
    # }

    # server {
    #   location /test {
    #     root /home/grant/www;
    #   }
    # }
}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

/etc/nginx/sites-available/default - 请注意,自从我安装此文件以来,它一直没有改变,并且是我所指的 root 指令所在的位置。

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # 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 index.htm index.nginx-debian.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/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

编辑:/etc/nginx/sites-enabled/default 中有一个软符号链接指向 /etc/nginx/sites-available/default

【问题讨论】:

  • 据我记得它在那里保留错误页面,也就是说,如果您点击 404、500,它将向您显示来自 /usr/share/nginx 的文件。配置似乎很好,我认为 @987654335 @ 应该在<server>:80/test.html 可用。
  • 很遗憾,错误页面不在/usr/share/nginx,尝试<server>:80/test/html也没有成功。
  • 不是test/html - test.html。 Root 定义开始查找文件的位置。以/var/www/html/test.html 的示例将返回/var/www/html/test.html。但是如果您访问/test/html,它将在/var/www/html/test/html 中查找索引文件。索引文件是那些用index 指令定义的文件。配置/etc/nginx/sites-available/default 也不包含在nginx.conf 中。您需要将其移动到/etc/nginx/sites-enabled 以便 NGINX 加载它(移动后不要忘记重新加载 NGINX)。现在是其他一些 vhost 正在处理答案,而不是我们讨论的那个。
  • 很抱歉,我在尝试<server>:80/test.html 时做了一个类型。我还编辑了问题以指定在/etc/nginx/sites-enabled 中存在指向/etc/nginx/sites-available/default 的符号链接。我也能够回答我自己的问题!感谢您的帮助,因为我只是在学习 nginx。
  • 不客气。这是您了解服务器名称的链接nginx.org/en/docs/http/request_processing.html

标签: nginx nginx-config


【解决方案1】:

好的,我通过学习有关 nginx 的新知识解决了这个问题。问题是 nginx.conf/etc/nginx/sites-enabled/default 中端口 80 的服务器块发生冲突,我不知道。尽管/etc/nginx/sites-enabled/default 被列为默认服务器(listen 80 default_server),但 nginx 使用了nginx.conf 中的服务器块,因为该服务器块具有优先于 default_server 的服务器名称指令(server_name kramericaindustries.hopto.org;)。 (是的,我是用这个域名来测试的。)nginx只使用一个服务器块来完成请求。

因为nginx.conf中的server块没有指定root,所以默认使用nginx路径前缀/usr/share/nginx不包含test.html。因此,请求失败。我将root /var/www/html; 添加到nginx.conf,现在一切正常。

【讨论】:

    猜你喜欢
    • 2014-12-13
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多