为什么nginx认为我的根目录是/usr/share/nginx,而不是我的配置状态下的/var/www/html?

为什么nginx认为我的根目录是/usr/share/nginx,而不是我的配置状态下的/var/www/html?,nginx,nginx-config,Nginx,Nginx Config,我是nginx的新手,正在努力掌握它的窍门。我一直在阅读文档,他们说如果使用指令root,它应该告诉nginx在哪里可以找到请求。例如,根据我的理解,root/var/www/html应该告诉nginx在目录/var/www/html中查找请求,但我的nginx实例没有这样做。我试图在名为test.html的目录中加载一个文件,但它试图在/usr/share/nginx中查找该文件。请注意,这是一个非常新的nginx安装,我对默认配置文件做了一些更改。我还想注意路径前缀设置为/usr/share

我是nginx的新手,正在努力掌握它的窍门。我一直在阅读文档,他们说如果使用指令
root
,它应该告诉nginx在哪里可以找到请求。例如,根据我的理解,
root/var/www/html
应该告诉nginx在目录
/var/www/html
中查找请求,但我的nginx实例没有这样做。我试图在名为
test.html
的目录中加载一个文件,但它试图在
/usr/share/nginx
中查找该文件。请注意,这是一个非常新的nginx安装,我对默认配置文件做了一些更改。我还想注意路径前缀设置为
/usr/share/nginx
,但我的理解是使用
root
指令应该覆盖它。我正在运行Ubuntu18.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
好的,我通过学习nginx的新知识解决了这个问题。问题是
nginx.conf
/etc/nginx/sites enabled/default
中端口80的服务器块发生冲突,我没有意识到这一点。虽然
/etc/nginx/sites enabled/default
被列为默认服务器(
listen 80 default_server
),但nginx使用的是
nginx.conf
中的服务器块,因为此服务器块具有优先于默认_服务器的服务器名指令(
server name kramericaindustries.hopto.org;
)。(是的,我使用这个域名进行测试。)nginx只使用一个服务器块来满足请求


由于
nginx.conf
中的服务器块未指定根,因此默认情况下它使用了nginx路径前缀,即
/usr/share/nginx
,该前缀不包含
test.html
。因此,请求失败。我添加了
root/var/www/html
nginx.conf
,现在一切都按预期工作。

据我记忆,它保留了错误页面,即如果你点击404500,它将显示
/usr/share/nginx
中的一个文件。配置似乎很好,我认为
test.html
应该在
:80/test.html
上可用。不幸的是,错误页面不在
/usr/share/nginx
上,尝试
:80/test/html
也不起作用。不是
test/html
-
test.html
。Root定义从何处开始查找文件。给定
/test.html
/var/www/html
示例,它将返回
/var/www/html/test.html
。但是如果访问
/test/html
,它将在
/var/www/html/test/html
中查找索引文件。索引文件是使用
Index
指令定义的文件。另外,
nginx.conf
中不包括config
/etc/nginx/sites available/default
。您需要将其移动到
/etc/nginx/sites enabled
,nginx才能加载它(移动后不要忘记重新加载nginx)。现在是其他vhost在处理答案,而不是我们讨论的答案。很抱歉,我在尝试时键入了
:80/test.html
。我还编辑了这个问题,指定在
/etc/nginx/sites enabled
中有一个指向
/etc/nginx/sites available/default
的符号链接。不过我也能回答我自己的问题!谢谢你的帮助,我只是在学习nginx。不客气。这里有一个链接供您了解服务器名称