Nginx 默认为 /usr/share/nginx/html

Nginx 默认为 /usr/share/nginx/html

这是一个开发网站。根据我今天早上的备份,推送到实时环境后就可以正常工作了。

系统

  1. Ubuntu 18.04

  2. Nginx

  3. PHP 7.2

  4. MYSQL

正在努力删除 TLSv1 表单证书路径。删除后测试一切正常。正在使用插件重新应用 API,以便它能够在正确的 URL 上回调。现在我只得到默认的 Nginx 页面。我已从备份恢复到新服务器,站点正在运行。我正在做分类,似乎无法弄清楚到底发生了什么。

以下是 NGINX 的错误日志:

2018/07/13 19:37:01 [error] 4593#4593: *206 open() "/usr/share/nginx/html/wp-admin/admin-ajax.php" failed (2: No such file or directory), client: 65.152.214.10, server: , request: "POST /wp-admin/admin-ajax.php HTTP/1.1", host: "smokingquartz.com", referrer: "https://smokingquartz.com/wp-admin/plugins.php"
2018/07/13 19:39:01 [error] 4593#4593: *211 open() "/usr/share/nginx/html/wp-admin/admin-ajax.php" failed (2: No such file or directory), client: 65.152.214.10, server: , request: "POST /wp-admin/admin-ajax.php HTTP/1.1", host: "smokingquartz.com", referrer: "https://smokingquartz.com/wp-admin/plugins.php"
2018/07/13 19:41:02 [error] 4593#4593: *213 open() "/usr/share/nginx/html/wp-admin/admin-ajax.php" failed (2: No such file or directory), client: 65.152.214.10, server: , request: "POST /wp-admin/admin-ajax.php HTTP/1.1", host: "smokingquartz.com", referrer: "https://smokingquartz.com/wp-admin/plugins.php"
2018/07/13 19:43:03 [error] 4593#4593: *215 open() "/usr/share/nginx/html/wp-admin/admin-ajax.php" failed (2: No such file or directory), client: 65.152.214.10, server: , request: "POST /wp-admin/admin-ajax.php HTTP/1.1", host: "smokingquartz.com", referrer: "https://smokingquartz.com/wp-admin/plugins.php"
2018/07/13 19:44:09 [error] 4593#4593: *218 open() "/usr/share/nginx/html/phpmyadmin/index.php" failed (2: No such file or directory), client: 96.88.66.233, server: , request: "POST /phpmyadmin/index.php HTTP/1.1", host: "smokingquartz.com"
2018/07/13 19:45:03 [error] 4593#4593: *220 open() "/usr/share/nginx/html/wp-admin/admin-ajax.php" failed (2: No such file or directory), client: 65.152.214.10, server: , request: "POST /wp-admin/admin-ajax.php HTTP/1.1", host: "smokingquartz.com", referrer: "https://smokingquartz.com/wp-admin/plugins.php"
2018/07/13 19:47:03 [error] 4593#4593: *222 open() "/usr/share/nginx/html/wp-admin/admin-ajax.php" failed (2: No such file or directory), client: 65.152.214.10, server: , request: "POST /wp-admin/admin-ajax.php HTTP/1.1", host: "smokingquartz.com", referrer: "https://smokingquartz.com/wp-admin/plugins.php"

看来我的根文件夹已更改为 /usr/share/nginx/html。这不是我在可用/启用的站点中配置的内容。

可用站点:

server {
## Basic Info ##
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name smokingquartz.com www.smokingquartz.com;
    index index.php index.html index.htm index.nginx-debian.html;
    root /var/www/html/smokingquartz/;

## WP Defender - Prevent PHP Execution ##
    # Stop php access except to needed files in wp-includes
    location ~* ^/wp-includes/.*(?<!(js/tinymce/wp-tinymce))\.php$ {
        internal; #internal allows ms-files.php rewrite in multisite to work
    }

    # Specifically locks down upload directories in case full wp-content rule below is skipped
    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
        }

    # Deny direct access to .php files in the /wp-content/ directory (including sub-folders).
    #  Note this can break some poorly coded plugins/themes, replace the plugin or remove this block if it causes trouble
    location ~* ^/wp-content/.*\.php$ {
        deny all;
    }            
## WP Defender - End ##

## Rewrite for sitemap ##
    rewrite ^/(.*/)?sitemap.xml /wp-content/uploads/sitemap.xml last;

## exact-matching loctation blocks ##
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(txt|xml|js)$ {expires 8d;}
    location ~* \.(css)$ {expires 8d;}
    location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac|eot|ttf|otf|woff|svg)$ {expires 8d;}
    location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {expires 8d;}
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }

## pass PHP scripts to FastCGI server ##
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php7.2-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;
    }
## GZIP ##  
    gzip on;
        gzip_comp_level    5;
        gzip_min_length    256;
        gzip_proxied       any;
        gzip_vary          on;

    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;
        # text/html is always compressed by gzip module}server{
listen [::]:443 ssl ipv6only=on ; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/smokingquartz.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/smokingquartz.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot}

我将其与备份文件进行了比较,它们是相同的。

这是 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 {
client_max_body_size 32M;
##
# 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;
##
# Humming Bird Cache
##


server{

rewrite ^/(.*/)?sitemap.xml /wp-content/uploads/sitemap.xml last;

location ~* \.(txt|xml|js)$ {
   expires 8d;
}

location ~* \.(css)$ {
    expires 8d;
}

location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac|eot|ttf|otf|woff|svg)$ {
    expires 10d;
}

location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
    expires 8d;
}
}
##
# Gzip Settings
##

# Enable Gzip compression
gzip          on;

# Compression level (1-9)
gzip_comp_level     5;

# Don't compress anything under 256 bytes
gzip_min_length     256;

# Compress output of these MIME-types
gzip_types
application/atom+xml
application/javascript
application/json
application/rss+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-font-opentype
application/x-font-truetype
application/x-javascript
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/eot
font/opentype
font/otf
image/svg+xml
image/x-icon
image/vnd.microsoft.icon
text/css
text/plain
text/javascript
text/x-component;

# Disable gzip for bad browsers
gzip_disable  "MSIE [1-6]\.(?!.*SV1)";

# 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/*;
}

如果您还有任何其他需要,请告诉我,这个问题让我很困惑。

****编辑更正拼写错误:“Nginix”改为“Nginx”****

答案1

在 Nginx 中,如果 uri 不匹配,location则通常默认查找/usr/share/nginx/html/(OpenResty 也是如此/usr/openresty/nginx/html/)。因此,它似乎与您的某个 URI 不匹配,因此会返回到默认位置。

找出默认位置的最简单方法是运行(正如@Snidhi 所评论的,该-V选项现在取代了-h用于此目的的选项):

nginx -V

可以通过获取响应中显示的路径--prefix并在其后附加“html”来找到默认位置。

如果您仅创建一个没有任何条目的最小 nginx.conf 文件location并以调试模式运行它,则可以看到这种行为:

# Put in file /tmp/nginx-test.conf
error_log /dev/stderr debug;
daemon off;

events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
    }
}

然后运行服务器:

sudo nginx  -c /tmp/nginx-test.conf

您将看到所有请求(例如在另一个窗口中:)

curl http://127.0.0.1:8080/test

尝试打开()文件/usr/share/nginx/html/

答案2

您有两个不同的server块。一个通过 HTTP 为您的 WordPress 网站提供服务。另一个由 certbot 创建,并提供没有什么关于HTTPS。你可以将相关配置添加到第二个server块中,或者将它们组合起来,以解决眼前的问题。

不幸的是,certbot 不太擅长编写 Web 服务器配置,我总是建议人们自己整理 nginx 配置,并在 certonly webroot 模式下运行 certbot。我发布了一个示例配置在本网站的另一个答案中,您可能会发现它很有用。

答案3

您应该禁用预先配置的默认网站已启用站点

/etc/nginx/sites-enabled/default

关于你的问题,你列出了可用站点, 不是已启用

除非您使用相同的默认文件并覆盖了您自己的虚拟主机的参数,否则您应该简单地从 sites-enabled 中删除符号链接:

sudo rm /etc/nginx/sites-enabled/default

并重新加载 nginx:

sudo systemctl reload nginx

答案4

感谢 Leo Gallego 建议我运行grep -R root /etc/nginx/*,我才能够找到default.conf中的文件/etc/nginx/conf.d。一旦我将其重命名为其他名称,一切便会按预期重新开始工作。

相关内容