摘要:nginx服务器下的反向代理的配置步骤比apache服务器稍简单,ngingx只需在需要代理站点配置代理规则就可以了。
nginx服务器下的反向代理的配置步骤比apache服务器稍简单,ngingx只需在需要代理站点配置代理规则就可以了。
(www.test1.com 代理 www.test2.com)
1、全站代理配置(首页也代理)
server { listen 80; server_name www.test1.com ; root "D:\PHP\WWW\nginx.com"; location / { index index.html index.htm index.php; #autoindex on; #反向代理配置 proxy_pass http://www.test2.com } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; 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; } }
2、全站代理配置(首页不代理)
server { listen 80; server_name www.test1.com ; root "D:\PHP\WWW\nginx.com"; #配置首页不代理 location = / { index index.html index.htm index.php } location / { index index.html index.htm index.php; #autoindex on; #反向代理配置 proxy_pass http://www.test2.com } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; 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; } }
3、目录代理配置
server { listen 80; server_name www.test1.com ; root "D:\PHP\WWW\nginx.com"; #目录代理 news目录代理 www.test2.com location /news/ { proxy_pass http://www.test2.com } location / { index index.html index.htm index.php; #autoindex on; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; 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; } }
4、指定类型文件代理配置
server { listen 80; server_name www.test1.com ; root "D:\PHP\WWW\nginx.com"; #指定文件类型代理配置(.shtml、.htm 类型文件代理 www.test2.com) location ~ .*\.(shtml|htm)$ { proxy_pass http://www.test2.com } location / { index index.html index.htm index.php; #autoindex on; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; 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; } }
nginx服务器的指定文件类型反向代理配置时,只配置一种文件类型时,重启服务器的时候报错,导致服务器重启失败,不知道是配置的时候出了点小差错还是这里必须两种以上的类型。