私信  •  关注

Richard Smith

Richard Smith 最近创建的主题
Richard Smith 最近回复了
3 年前
回复了 Richard Smith 创建的主题 » 两个nginx中服务器设置的关系是什么。conf和proxy。形态?

Nginx选择一个 server 块来根据 listen server_name 指令。

如果找不到匹配的服务器名称,将使用该端口的默认服务器。

在您问题中的配置中 服务器 封锁 proxy.conf 首先遇到,因此它实际上成为端口80的默认服务器。

这个 服务器 封锁 nginx.conf 将只匹配使用正确主机名的请求,即。 http://localhost

看见 this document 详细信息。

3 年前
回复了 Richard Smith 创建的主题 » Nginx Strip URL参数

如果需要递归,可以使用 rewrite...last 从一年之内 location 块在生成内部服务器错误之前,Nginx只能容忍少量的递归(可能是十次迭代)。

例如:

location / {
    if ($args ~ ^(?<prefix>.*)&(?:unwanted1|unwanted2)=[^&]*(?<suffix>&.*)?$ ) {
        rewrite ^ $uri?$prefix$suffix? last;
    }
    ...
}

请注意,您需要使用命名捕获,因为当 rewrite 语句被评估。

注意 重写 需要拖尾 ? 防止将现有参数附加到重写的URI。看见 this document 详细信息。

您需要使用正则表达式 location 块捕获内部重定向到正确的 index.php 位置 使用 ~ ~* 修饰语。见 this document

正则表达式是按顺序计算的,因此 位置 对于 \.php$ 必须 置于 您正在插入。否则,PHP文件将被下载而不是执行。

例如:

location ~ \.php$ {
    ...
}
location ~ ^/([^/]+)/([^/]+)/ {
    try_files $uri $uri/ /$1/$2/index.php$is_args$args;
}
6 年前
回复了 Richard Smith 创建的主题 » NGINX只为特定目录和索引文件指定变量

如果要基于原始请求设置变量,应使用 map 指令与 $request_uri 变量。见 this document 详细情况。

例如:

map $request_uri $skip_cache {
    default      1;
    ~^/user/     0;
    ~^/objects/  0;
}
server {
    ...
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    ...
}
6 年前
回复了 Richard Smith 创建的主题 » 如果url包含“/”,则Nginx重定向?&”

这个 location rewrite 指令对 normalised URI 它不包括查询字符串(来自 ? 继续)。

重定向以结尾的所有uri ?& ,你可以测试 $args 变量。例如:

if ($args = "&") { return 301 $uri; }

或者,只重定向URI /?& ,你可以测试 $request_uri 变量。例如:

if ($request_uri = "/?&") { return 301 /; }
6 年前
回复了 Richard Smith 创建的主题 » 如何使用nginx提供基于URL的角度应用程序?

“something”太多。文件的路径是通过将URI附加到 root . 见 this document 详细情况。

在您的配置中,文件术语 $uri $uri/ 将不匹配安迪文件。

如果您删除 something 从你的 值,您需要将其添加到 index.html 文件元素。

例如:

location /something/ {
    root /var/www/xyz.com/html;
    try_files $uri $uri/ /something/index.html;
}

就我个人而言,我没有附加 =404 ,但它没有坏处,我知道很多角度的教程似乎认为这是必要的。见 this document 详细情况。

6 年前
回复了 Richard Smith 创建的主题 » nginx-i将头变量作为查询参数传递到上游

如果您有一个名为 userid ,它将在名为 $http_userid .

您可以使用 rewrite...break 语句。

例如:

location /test {
    rewrite ^(.*)$ $1?userid=$http_userid break;
    proxy_pass http://localhost:8080;
}

this document 详情。

6 年前
回复了 Richard Smith 创建的主题 » 对nginx中的尾随斜杠行为有点困惑

他们完全不同。

在第一 proxy_pass 语句中包含一个值为的uri参数 / . 在你没有的那一刻。

当你付出 主动传球 uri参数(在前缀内 location ,它将请求的uri转换为类似于 alias 函数,其中 位置 指令被替换为uri参数的值。例如 /myapi/foo 变成 /foo 在上游通过之前。

如果你不提供 主动传球 有了uri参数,就不会发生转换,请求 /升/升 在上游传递时保持不变。

this document 详情。

6 年前
回复了 Richard Smith 创建的主题 » 位置匹配后nginx重写

这个 ^/$ 与uri不匹配 /12345678 -它只匹配uri / .

您可以使用:

rewrite ^ /index.html break;

这个 ^ 只是许多匹配任何事物的正则表达式之一。这个 break 后缀导致重写的uri在同一个 location 封锁。见 this document 详细情况。


您可以使用 try_files 指令:

location ~ "^/[\d]{8}" {
    root /usr/share/nginx/html;
    try_files /index.html =404;
}

这个 =404 从句从未达到 index.html 一直存在-但是 试用文件 必须至少有两个参数。见 this document 详细情况。

5 年前
回复了 Richard Smith 创建的主题 » 如何调试和修复nginx错误的位置重定向问题?

数字捕获 $1 由最后一个设置 正则表达式 待评估。在第二种情况下,在 map 语句已计算 之后 中的正则表达式 location 声明。

解决方案是改用命名捕获。

例如:

map $http_referer $be_pool {                                                                                                                                                                                                                                                                                                                                       
    default                  be;                                                                                                                                                                                                                                                                                                                                   
    "~a\.com\/.*\/0\/.*"     be_demo;                                                                                                                                                                                                                                                                                                                
} 
server {
    ...
    location ~ ^/capi/(?<myuri>.*)$ {                                                                                                                                                                                                                                                                                                                                               
        proxy_pass http://$be_pool/$myuri;                                                                                                                                                                                                                                                                                                                             
    } 
}
6 年前
回复了 Richard Smith 创建的主题 » 需要在nginx中使用不同根目录的帮助

如果uri以 /clientfolder 位于 /home/clientfolder/website ,您应该使用 alias 指令。

例如:

location /clientfolder {
    alias /home/clientfolder/website;
}

this document 详情。

6 年前
回复了 Richard Smith 创建的主题 » nginx拒绝对文件夹的所有访问,但php脚本不受规则影响

正则表达式位置块优先于前缀位置块,因此 .php 规则中不包含文件。

使用 ^~ 使前缀位置优先于正则表达式位置块的修饰符。

例如:

location ^~ /config { 
    return 403; 
}

this document 详情。

6 年前
回复了 Richard Smith 创建的主题 » 在没有安装卷的nginx的情况下,是否可以将请求传递给php fpm?

index index.php / /index.php

location /

location / {
    rewrite ^ /index.php last;
}
location ~* \.php$ {
    root /var/www/html/public; 
    client_max_body_size 0;

    include fastcgi_params;
    fastcgi_pass php-fpm:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $document_root;
}
6 年前
回复了 Richard Smith 创建的主题 » 如何在nginx服务器上的不同位置托管多个HTML文件

要将nginx服务器配置为监听特定端口,请使用 listen 指令。见 this document 详情。

例如:

server {
    listen 8080;
    root /var/www/ex1.com;
}
server {
    listen 8081;
    root /var/www/ex2.com;
}

网址 http://<ip_address>/ex1 http://<ip_address>/ex2 将由相同的 server 阻止,监听端口80。

您需要使用 alias 指令而不是 root 指令,因为不能通过简单地将某个值与URI串联来创建本地文件的路径。

例如:

server {
    listen 80;

    location /ex1 {
        alias /var/www/ex1.com;
    }
    location /ex2 {
        alias /var/www/ex2.com;
    }
}

注意两个 location 价值与 别名 值应该有一个尾随 / 或者两者都没有尾随 / . 见 this document 详情。

6 年前
回复了 Richard Smith 创建的主题 » nginx将与参数匹配的URL的所有请求重定向到子域

example.com/api/foo?bar api.example.com/api/foo?bar

location ^~ /api {
    return 307 https://api.exemple.com$request_uri;
}

$request_uri /api/

^~ location this document this link

6 年前
回复了 Richard Smith 创建的主题 » nginx 301从旧网站重定向到新闻网站

处理 example.com www.example.com 不同的是,您应该拆分现有的 server 分为两块,并将所需的 return 陈述到其中一个。

例如:

server {
    listen      443 ssl http2;
    server_name example.com;

    ssl_certificate      /home/newsite/conf/web/ssl.newsite.com.pem;
    ssl_certificate_key  /home/newsite/conf/web/ssl.newsite.com.key;

    return 301 https://www.newsite.com$request_uri;
}

server {
    listen      443 ssl http2;
    server_name www.example.com;

    ssl_certificate      /home/newsite/conf/web/ssl.newsite.com.pem;
    ssl_certificate_key  /home/newsite/conf/web/ssl.newsite.com.key;

    root        /home/newsite/web/newsite.com/public_html/pub;
    index       index.php;
    autoindex   off;
    charset     UTF-8;
    error_page  404 403 = /errors/404.php;
    add_header  "X-UA-Compatible" "IE=Edge";

    ...
    ...
    ...
}
6 年前
回复了 Richard Smith 创建的主题 » nginx重写PHP分页规则

rewrite 语句按顺序计算,当包含 旗帜 与URI匹配。见 this document 详情。

你应该把更具体的 正则表达式 在不太具体的问题之前。

例如:

location /category/ {
    rewrite ^(.*[^/])$ $1/ permanent;
    rewrite  ^/category/(.*)/page/(.*)/$ /index.php?slug=$1&type=category&page=$2 last;
    rewrite  ^/category/(.*)/$ /index.php?slug=$1&type=category last;
}

使 正则表达式 更具体的说,你可以使用 ([^/]+) 捕捉鼻涕虫 (\d+) 以捕获页码。

6 年前
回复了 Richard Smith 创建的主题 » 在nginx中添加从'url.org/`到'url.org的重定向`

你无法检测到 // 用一个 location 规则,因为URI已经被规范化,并且连续出现多次 / 已经折叠成一个 / . 您将需要使用由 $request_uri 变量。

例如:

if ($request_uri ~ (.*/)/$) { return 301 $1; }
6 年前
回复了 Richard Smith 创建的主题 » 使用相同根目录的nginx虚拟目录出现404错误

这个 root 指令通过其值与当前URI的简单连接来构造文件的路径。所以你的第二个定位块在 /var/www/php/site/sitea/index.php .

这个 alias 前缀位置中的指令将用 别名 价值。见 this document 更多。

location /sitea {
    alias /var/www/php/site;
    ...
}

所以上面的位置块将查找URI /sitea/index.php /var/www/php/site/index.php .

两者都是 别名 指令设置一个名为 $request_filename 到文件的路径。

在php块中,使用 $document_root$fastcgi_script_name 通知php-fpm文件的路径。这与 但不能 别名 .

fastcgi_param SCRIPT_FILENAME $request_filename;

以上两种方法都适用 别名 对于不处理路径信息的PHP块(如您的)。