AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1429016
Accepted
Axel
Axel
Asked: 2019-04-25 01:33:34 +0800 CST2019-04-25 01:33:34 +0800 CST 2019-04-25 01:33:34 +0800 CST

Wordpress 永久链接在 Nginx 中不起作用

  • 772

我正在将我的 wordpress 博客从 Apache 迁移到 Nginx。我已经尝试了多个教程来使永久链接正常工作,但对我来说没有任何效果。我的网站结构是这样的:

main site -> www.localhost.com

wordpress blog -> www.localhost.com/blog

网站在/var/www/html,wordpress安装在/var/www/html/blog

我阅读了多篇文章并观看了多个视频,但似乎没有任何效果。请让我知道我哪里出错了。

我定义了两个服务器块,/etc/nginx/sites-available/default 一个用于主站点,一个用于博客。

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

    server_name _;

       charset utf-8;
       error_page 404 /404.php;

       location /article {
         rewrite ^/article.* / redirect;
       }

       #location / {
        # try_files $uri $uri/ /loadpage.php?$args; 
       #}

       location ~ \.html$ {
         try_files $uri /courses/index.php?$args;
       }





    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                include fastcgi_params;

    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
}



server {
    listen 80;
    listen [::]:80;
    root /var/www/html/blog;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    location /blog/ {
    try_files $uri $uri/ /blog/index.php?$args;        
    }


    location ~ \.php$ {
    fastcgi_split_path_info  ^(.+\.php)(/.+)$;
    fastcgi_index            index.php;
 #  fastcgi_pass             unix:/var/run/php/php7.1-fpm.sock; #Ubuntu 17.10
    fastcgi_pass             unix:/var/run/php/php7.0-fpm.sock; #Ubuntu 17.04
    include                  fastcgi_params;
    fastcgi_param   PATH_INFO       $fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}
nginx apache-http-server
  • 1 1 个回答
  • 453 Views

1 个回答

  • Voted
  1. Best Answer
    Thomas
    2019-04-25T04:06:38+08:002019-04-25T04:06:38+08:00

    通过为同一个域创建多个server块,您实际上在虚拟服务器之间造成了冲突。server块定义 Nginx 将监听什么。如果给定端口有多个虚拟服务器,请求将被传递到具有最佳匹配的虚拟服务器server_name。通过拥有多个相同的虚拟服务器,Nginx 将无法确定将其传递到何处。

    您的主站点和 WordPress 在同一个域(和同一个端口)下,因此应该进入一个虚拟服务器。您使用location块将它们分开。

    注意Nginx 块选择算法,正则表达式块总是在普通块之前被评估。在您的示例中,所有以结尾的内容.html都将发送到您的主站点。当 WordPress 尝试使用 URI 的结尾时,这可能会干扰 WordPress .html。为防止这种情况,您可以使用嵌套的位置块。通过将正则表达式位置块移动到/其中,只有在父位置匹配时才会对其进行评估。当收到请求时/blog,/bloglocation 块将是最匹配的块,因此将被选中/。因为正则表达式块现在嵌套在那里,所以不会对 WordPress 的请求进行评估。

    # 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.php index.html index.htm index.nginx-debian.html;
    
        server_name _;
    
        charset utf-8;
        error_page 404 /404.php;
    
        location /article {
            rewrite ^/article.* / redirect;
        }
    
        location / {
            location ~ \.html$ {
                try_files $uri /courses/index.php?$args;
            }
    
            try_files $uri $uri/ /loadpage.php?$args;
        }
    
        location /blog/ {
            try_files $uri $uri/ /blog/index.php?$args;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
        #
        #   # With php7.0-cgi alone:
        #   fastcgi_pass 127.0.0.1:9000;
            # With php7.0-fpm:
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                    include fastcgi_params;
    
        }
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny all;
        }
    }
    
    • 1

相关问题

  • 传入的主机头加倍

  • 续订 letsencrypt SSL 证书后,服务器仅返回响应代码 400

  • 是否可以将 Apache 配置为同一端口上的静态 Web 服务器和负载平衡器?

  • apache2 可以在没有 conf 文件的情况下工作吗?

  • Nextcloud 共享在 nginx 反向代理中不起作用

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    Windows 10 服务称为 AarSvc_70f961。它是什么,我该如何禁用它?

    • 2 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Marko Smith

    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)

    • 4 个回答
  • Marko Smith

    我如何知道 Windows 安装在哪个驱动器上?

    • 6 个回答
  • Martin Hope
    Albin 支持结束后如何激活 WindowsXP? 2019-11-18 03:50:17 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    Kagaratsch Windows 10 删除大量小文件的速度非常慢。有什么办法可以加快速度吗? 2019-09-23 06:05:43 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    Inter Sys Ctrl+C 和 Ctrl+V 是如何工作的? 2019-05-15 02:51:21 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve