1

从现在开始,我一直在为此苦苦挣扎,并想问问社区。

我最近开始研究数字海洋和 nginx。我之前使用的是apache。

我有一个简单的 wordpress 网站,可以在本地正常工作,但是当使用 nginx 在 droplet 服务器上推送时,该网站无法正常工作。

登陆页面工作正常,但是当我路由到不同的链接时,我从 nginx 收到 404 错误页面。

我觉得这是因为在 nginx 中没有获取 htaccess 文件,我必须在 nginx 中重写规则但没有任何效果。

这是我的 nginx.conf 文件的配置文件,sites-available/default 文件

网站可用/默认

server {
    listen 80;
    server_name martinschildrenacademy.com www.martinschildrenacademy.com;
    return 301 https://$host$request_uri;
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location / {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
         }
    }

    # Default server configuration
    #
    server {
    #       listen 80 default_server;
    #       listen [::]:80 default_server;
    listen 443 ssl;
    server_name martinschildrenacademy.com www.martinschildrenacademy.com;

    ssl_certificate /etc/letsencrypt/live/martinschildrenacademy.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/martinschildrenacademy.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    # 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.php  index.html index.htm index.nginx-debian.html;

    server_name martinschildrenacademy.com;

    # 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.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;
    }
  }

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

有什么建议可以帮助我解决这个问题吗?

4

2 回答 2

1

您的站点可用/默认文件中有两个服务器块。第一个服务器块接收 HTTP 请求,然后用重定向指令响应第二个服务器块定义的 HTTPS 版本。您只需将前 3 行保留在第一个块内,然后像这样删除其余行:

server {
    listen 80;
    server_name martinschildrenacademy.com www.martinschildrenacademy.com;
    return 301 https://$host$request_uri;
}

WordPress 的 Nginx 配置必须具有以下位置块才能正常工作:

location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when using query string
        try_files $uri $uri/ /index.php?$args;
}

因此,您必须将 location 块插入到第二个 server 块中。你做到了。它奏效了。

于 2020-09-21T16:02:37.993 回答
0

刚刚得到这个工作。

不知道为什么,但我只是移动了这条线

location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when using query string
        try_files $uri $uri/ /index.php?$args;
}

在 default.config 文件中从上到下的服务器块。它奏效了。

于 2020-09-10T07:04:52.267 回答