我有一个域 (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;
# …Run Code Online (Sandbox Code Playgroud)