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
    • 最新
    • 标签
主页 / server / 问题 / 827419
Accepted
miken32
miken32
Asked: 2017-01-20 15:45:55 +0800 CST2017-01-20 15:45:55 +0800 CST 2017-01-20 15:45:55 +0800 CST

Nginx 和不同类型的别名

  • 772

我的网络服务器上有以下文件:

/var/www/html/--+
                |
                +-misc--+
                |       |
                |       +-misc1--+
                |       |        |
                |       |        +-index.html
                |       |
                |       +-misc2--+
                |       |        |
                |       |        +-index.php
                |       |
                |       +-misc3.php
                |
                +-wordpress--+
                             |
                             +-index.php

我设置了 Nginx,以便http://example.com/进入我的 Wordpress 安装。在我之前的(Apache)设置中,我能够轻松地创建别名来指向其中的项目,misc但我不确定如何在 Nginx 中执行此操作。

    index index.php index.html;
    root /var/www/html/wordpress;

    location ~ [^/]\.php(/|$) {
        limit_except GET POST {}
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        fastcgi_buffer_size 16k;
        fastcgi_buffers 16 16k;

        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param    PATH_INFO          $fastcgi_path_info;
        fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
        fastcgi_param    SERVER_NAME        $host;
        fastcgi_param    HTTP_PROXY         "";

        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }

    location / {
        try_files $uri $uri/ /index.php;
        limit_except GET {}
    }

我想要的是:

  • http://example.com/加载 /var/www/html/wordpress/index.php
  • http://example.com/misc1加载 /var/www/html/misc/misc1/index.html
  • http://example.com/misc2加载 /var/www/html/misc/misc2/index.php
  • http://example.com/misc3.php加载 /var/www/html/misc/misc3.php

我试过的:

# http://example.com/misc1 shows index.html but anything else in the folder is 404
location /misc1 {
    alias /var/www/html/misc/misc1;
}

# http://example.com/misc1 gives 403, http://example.com/misc1/index.html gives 404
location /misc1/ {
    alias /var/www/html/misc/misc1;
}

# shows Wordpress 404 page
location /misc1/* {
    alias /var/www/html/misc/misc1;
}

# gives 403 error
location ~ /misc1 {
    alias /var/www/html/misc/misc1;
}

我尝试过的任何东西都没有对 PHP 产生任何影响,它不起作用。

nginx
  • 2 2 个回答
  • 686 Views

2 个回答

  • Voted
  1. Tim
    2017-01-20T15:56:39+08:002017-01-20T15:56:39+08:00

    不知道你为什么使用别名。试试这个,它没有经过测试,但它应该让你至少更接近你想要达到的目标。如果它不起作用,请评论为什么它不起作用的详细信息,并显示适用的日志和卷曲。

    root /var/www/html/misc
    try_files $uri $uri/; # NB This can be specified at the server or location level
    
    location / {
      root /var/www/html/wordpress;
      try_files $uri $uri/ /index.php?$args;
    }
    
    location /misc1/ {
      root /var/www/html/misc;
    }
    
    location /misc2/ {
      root /var/www/html/misc;
    }
    

    编辑 基于评论“只要我在服务器范围内更改根指令,我就会在我的 Wordpress 网站上得到 404,就好像它忽略了位置范围上的根指令一样。” 你可以试试这个。当我在根目录中有一个自定义 PHP 应用程序,而 Wordpress 在一个子目录中时,我会使用这种技术。

    root /var/www/html/;
    location / {
      try_files $uri $uri/ /wordpress/index.php?$args;
    }
    
    • 1
  2. Best Answer
    miken32
    2017-01-21T10:12:52+08:002017-01-21T10:12:52+08:00

    事实证明,location基于正则表达式的块总是会覆盖其他块location。我的配置有两个正则表达式位置:用于启用静态资源缓存的块,以及用于启用 PHP 的块。由于其中一个块与所有正在服务的内容匹配,因此每个其他位置块都被忽略了!

    我最终做的是嵌套位置块,将静态文件和 PHP 块放在位置块内。现在我的配置如下所示:

    php.inc

    location ~ [^/]\.php(/|$) {
        limit_except GET POST {}
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
    
        fastcgi_buffer_size 16k;
        fastcgi_buffers 16 16k;
    
        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param    PATH_INFO          $fastcgi_path_info;
        fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
        fastcgi_param    SERVER_NAME        $host;
        fastcgi_param    HTTP_PROXY         "";
    
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    

    静态公司

    location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }
    

    例子.conf

    index index.php index.html;
    root /var/www/html/wordpress;
    
    location / {
        try_files $uri $uri/ /index.php;
        limit_except GET {}
        include conf.d/php.inc;
        include conf.d/static.inc;
    }
    
    location /misc1 {
        root /var/www/html/misc/;
        include conf.d/static.inc;
    }
    
    location /misc2 {
        root /var/www/html/misc/;
        include conf.d/php.inc;
        include conf.d/static.inc;
    }
    
    location /misc3.php {
        root /var/www/html/misc/;
        include conf.d/php.inc;
    }
    
    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

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

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve