测试的文件都放在了附件中,下载附件打开test.html查看
以下nginx代理配置均自己测试通过
测试内容为:
访问http://127.0.0.1:8011/p/时,实际访问的是http://127.0.0.1:8010/
访问http://127.0.0.1:8011/p/pl/时,实际访问的是http://127.0.0.1:8010/a/
相关配置如下:
8011的nginx配置:
server {
listen 8011;
server_name t.local;
root "E:/Software/phpstudy_pro/WWW/t";
location / {
index index.php index.html error/index.html;
error_page 400 /error/400.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 509 /error/509.html;
error_page 510 /error/510.html;
include E:/Software/phpstudy_pro/WWW/t/nginx.htaccess;
autoindex off;
}location ~ ^/p/(.*)$ {
proxy_pass http://127.0.0.1:8010/$1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 这里的$1是一个正则表达式捕获的变量,代表/p/后面的所有内容
}
# 如果需要的话,可以添加额外的location来处理特定文件类型
# 例如:
# location ~ \.php$ {
# # PHP文件的代理设置
# proxy_pass http://127.0.0.1:8080;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# include fastcgi_params;
# }
# 如果需要处理静态文件,例如图片
# location ~ ^/p/\.(jpeg|jpg|png|gif)$ {
# proxy_pass http://127.0.0.1:8080/$1;
# }# 顺序不能和location ~ ^/p/(.*)$颠倒,否则匹配后不走到下面
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9006;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
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;
include fastcgi_params;
}}
8010的nginx配置:
server {
listen 8010;
server_name p.local;
root "E:/Software/phpstudy_pro/WWW/p";location / {
index index.php index.html error/index.html;
error_page 400 /error/400.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 509 /error/509.html;
error_page 510 /error/510.html;
include E:/Software/phpstudy_pro/WWW/p/nginx.htaccess;
autoindex on;
}location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9006;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
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;
include fastcgi_params;
}location /pl/ {
# 尝试列出目录内容
# autoindex on;
# 指定目录的根路径
alias "E:/Software/phpstudy_pro/WWW/p/a/";
# 禁用缓存
# add_header Cache-Control no-store,no-cache,must-revalidate,proxy-revalidate;
# add_header Pragma no-cache;
# add_header Expires 0;
# 代理到后端PHP-FPM服务器
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9006; # PHP-FPM默认监听9000端口
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;# 处理No input file specified错误
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SCRIPT_FILENAME $request_filename;
}# 其他请求直接代理到/a/
proxy_pass http://127.0.0.1:8010/a/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}# location / {
# try_files $uri $uri/ /index.php$is_args$args;
# }
# location ~ \.php(.*)$ {
# fastcgi_pass 127.0.0.1:9006;
# fastcgi_index index.php;
# include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# # include fastcgi.conf;
# }}
可实现使用nginx配置代理,访问一个目录或域名时实际访问到另一个目录或域名