Nginx-proxy_pass-特定位置不起作用

Nginx-proxy_pass-特定位置不起作用

我正在尝试使用 proxy_pass 屏蔽 Nginx 的远程 URL

我想加载staging.saas.localhost/_当浏览器 URL 为saas.localhost/uk_staging

由于某种原因,saas.本地主机不起作用,不起作用的意思是位置似乎被忽略了。

saas.localhost/uk_staging由应用程序处理,而不是来自staging.saas.localhost/_在我看来,即使对于saas.localhost/uk_staging使用的位置是 location ~ .php$

我创建了第二个域名t.saas.本地主机并且它按预期运行

t.saas.本地主机域名运行正常。

t.saas.localhost/uk_staging正在显示staging.saas.localhost/_ t.saas.localhost/anything_else正在显示google.co.uk/

这是我当前的 Nginx 配置:

server {
    listen 80;
    server_name   saas.localhost www.saas.localhost staging.saas.localhost;
    root /codebase/saas;
    index index.php index.html index.htm;

    location /uk_staging {
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://staging.saas.localhost/_;
    }

    if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?action=$1 last;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/tmp/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_read_timeout 600;
    }
}

server {
    listen 80;
    server_name  t.saas.localhost;
    root /codebase/saas;
    index index.php index.html index.htm;
    location /uk_staging {
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://staging.saas.localhost/_;
    }
    location / {
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://google.co.uk;
    }
}

答案1

为什么要使用proxy_pass指令重定向到同一个虚拟主机?!

此外,nginx 以你可能意想不到的方式选择匹配位置。请阅读以下内容:Nginx 重写规则 403 错误

if如果可以避免的话最好不要使用。

server {
    listen 80;
    server_name   saas.localhost www.saas.localhost staging.saas.localhost;
    root /codebase/saas;
    index index.php index.html index.htm;

    location ^~ /uk_staging {
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://staging.saas.localhost/_; # What are you doing here ??!!
    }

    #avoid rewrite for static content
    location ~* \.(js|jpg|png|css|htm|html|gif|txt|swf|mpg|mp4|avi)$ {
            expires 30d;
    }


    location / {
        rewrite ^(.*)$ /index.php?action=$1 last;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/tmp/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_read_timeout 600;
    }
}

相关内容