为什么 nginx 认为我的根目录是 /usr/share/nginx 而不是我配置状态下的 /var/www/html?
Why does nginx think my root directory is /usr/share/nginx and not /var/www/html as my configuration states?
我是 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/(.*)$ / 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;
)优先于 default_server。 (是的,我就是用这个域名做测试的。)nginx只用一个服务器块来完成请求。
因为nginx.conf
中的server块没有指定root,所以默认使用nginx路径前缀/usr/share/nginx
,不包含test.html
。因此,请求失败。我将 root /var/www/html;
添加到 nginx.conf
,现在一切正常。
我是 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/(.*)$ / 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;
)优先于 default_server。 (是的,我就是用这个域名做测试的。)nginx只用一个服务器块来完成请求。
因为nginx.conf
中的server块没有指定root,所以默认使用nginx路径前缀/usr/share/nginx
,不包含test.html
。因此,请求失败。我将 root /var/www/html;
添加到 nginx.conf
,现在一切正常。