首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么nginx认为我的根目录是/usr/share/nginx,而不是/var/www/html作为我的配置状态?

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

Stack Overflow用户
提问于 2021-03-12 04:16:46
回答 1查看 6.3K关注 0票数 3

我是新来的,并试图掌握它的诀窍。我一直在阅读文档,他们说如果使用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 -请注意此文件没有未注释的根指令

代码语言:javascript
运行
复制
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指令的位置。

代码语言:javascript
运行
复制
##
# 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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-12 07:19:03

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

因为nginx.conf中的服务器块没有指定根,它默认使用nginx路径前缀,即/usr/share/nginx,它不包含test.html。因此,请求失败。我将root /var/www/html;添加到nginx.conf中,现在一切都如愿以偿。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66594111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档