And*_*asa 5 nginx reverse-proxy proxypass
我有一个域 (example.com) 和一个子域 (sub.example.com),它们都托管在同一台服务器上。我想代理一个请求,所以在http://sub.example.com/api/上请求的任何文件都将在http://example.com/api/上请求(连同参数) 这是我的 nginx 代码sub.example.com:
location /api {
rewrite /api/(.*) /api/$1 break;
proxy_pass http://example.com/api;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
}
Run Code Online (Sandbox Code Playgroud)
这对我不起作用。有效的是:
location ~* ^/api/(.*)$ {
rewrite ^/api/(.*)$ http://example.com/api/$1 last;
}
Run Code Online (Sandbox Code Playgroud)
编辑:我忘了说我的服务器定义包含更多关于 Yii 框架和 php 文件解析的规则。这是完整的服务器语句:
server {
listen 80;
server_name sub.example.com;
access_log /var/log/nginx/sub.example.com.access_log main;
error_log /var/log/nginx/sub.example.com.error_log info;
root /var/www/localhost/htdocs/sub.example.com;
index index.php index.html index.htm default.html default.htm;
# location ~* ^/api/(.*)$ {
# rewrite ^/api/(.*)$ http://example.com/api/$1 last;
# }
location /api {
rewrite /api/(.*) /api/$1 break;
proxy_pass http://example.com/api;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
break;
}
location / {
if (-f $request_filename) {
#expires max;
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?/$1 last;
}
}
location /index.php {
include /etc/nginx/fastcgi.conf;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path-to/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass 127.0.0.1:9000;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi.conf;
fastcgi_index index.php;
fastcgi_intercept_errors on; # for easier debug
}
}
Run Code Online (Sandbox Code Playgroud)
您将初始请求Host标头sub.example.com传递给example.com:
proxy_set_header Host $http_host;
Run Code Online (Sandbox Code Playgroud)
代理请求时,它可能会落入sub.example.com虚拟主机或默认虚拟主机,而不是example.com虚拟主机,具体取决于您的设置。
删除此行。
| 归档时间: |
|
| 查看次数: |
7555 次 |
| 最近记录: |