nginx/apache 反向代码实用配置

  有时需要使用反向代理来进行跨域问题的解决,有两个可解决: nginx, apache 。

      话不多说,就让我们来看一下,怎样配置两个反向代理吧!

一、 nginx 配置最为常用:(安装方式请自行百度下。其中win直接拿来就用!linux中则需要自己定制模块安装!)

复制代码
server {
        listen 8088;
        server_name localhost 127.0.0.1 a.test;
        
    access_log  logs/localhost2.access.log  main;
    
        index index.html index.htm index.php;

        location ~ /test-xxx-api {
                proxy_redirect off;
                proxy_set_header Host $host;
                #proxy_set_header Content-Type   application/json;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                #add_header Access-Control-Allow-Origin *;
                proxy_set_header Access-Control-Allow-Origin *;
                proxy_pass http://10.40.10.159:49614;
        }
        
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root            D:/xampp/htdocs/;
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index    index.php;
            fastcgi_param    SCRIPT_NAME        $fastcgi_script_name;
            # fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param    SCRIPT_FILENAME D:/xampp/htdocs/$fastcgi_script_name;
            include            fastcgi_params;
        }

        location / {
            add_header Access-Control-Allow-Origin *;
            root D:/xampp/htdocs/;
            try_files $uri $uri/ /index.html;
            # try_files $uri $uri/ /index.html;
            index  index.php index.html index.htm;
        }
        
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }

}
复制代码

 

 

另外,日志格式可以调整为:

    log_format main '$request_time $upstream_response_time $remote_addr - [up_addr:$upstream_addr] [$time_local] '
                    '"$host" "$request" $status $bytes_sent '
                    '"$http_referer" "$http_user_agent" "$gzip_ratio" "$http_x_forwarded_for" - "$server_addr" ';

 

日志详细参数如下:

复制代码
【Ngnix 配置】 下载直接使用, default.conf
 相应的命令:start nginx.exe 命令了。
nginx.exe -s stop                   //停止nginx
nginx.exe -s reload                //重新加载nginx
nginx.exe -s quit                     //退出nginx
参数                      说明                                         示例
$remote_addr             客户端地址                                    211.28.65.253
$remote_user             客户端用户名称                                --
$time_local              访问时间和时区                                18/Jul/2012:17:00:01 +0800
$request                 请求的URI和HTTP协议                           "GET /article-10000.html HTTP/1.1"
$http_host               请求地址,即浏览器中你输入的地址(IP或域名)     www.wang.com 192.168.100.100
$status                  HTTP请求状态                                  200
$upstream_status         upstream状态                                  200
$body_bytes_sent         发送给客户端文件内容大小                        1547
$http_referer            url跳转来源                                   https://www.baidu.com/
$http_user_agent         用户终端浏览器等信息                           "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocol            SSL协议版本                                   TLSv1
$ssl_cipher              交换数据中的算法                               RC4-SHA
$upstream_addr           后台upstream的地址,即真正提供服务的主机地址     10.10.10.100:80
$request_time            整个请求的总时间                               0.205
$upstream_response_time  请求过程中,upstream响应时间                    0.002
复制代码

 

# 开启 php-cgi 程序
# 开启 php-cgi 程序
$#> php-cgi -b 9000 -q
# :: 打开cgi -b指定端口 -c指定php.ini的位置
# D:/xampp/php/php-cgi.exe -b 127.0.0.1:9000 -c D:/xampp/php/php.ini

# NGINX 提示Nginx PHP “No input file specified”错误的解决办法, cgi
原理:
    任何对.php文件的请求,都简单地交给php-cgi去处理,但没有验证该php文件是否存在。
    PHP文件不存在,没办法返回普通的404错误,它返回 一个404,并带上一句”No input file specified”
    另外,还可能跟 路径或者 权限有关系,或者SCRIPT_FILENAME 变量没有被正确的设置(这在nginx是最常见的原因)。
解决办法:
1 打开文件: usr/local/php/etc/php.ini,修改以下配置
把cgi.fix_pathinfo=0           改为    cgi.fix_pathinfo=1
把 ;cgi.force_redirect=1     改为    cgi.force_redirect=0

 

如遇问题,打开 logs/error.log 查看情况!

 

二、 apache 配置,虽然不是很常用,但是在你搞不定 nginx 时,还是可以派上用场的:

其中安装方式:win中可直接使用集成包安装!linux中则需要自定义模块安装!

1. 打开 proxy配置,端口监听打开:

复制代码
LoadModule proxy_module modules/mod_proxy.so; 打开代理模块
LoadModule proxy_http_module modules/mod_proxy_http.so; 打开http代理模块

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80
Listen 49614
复制代码

 

2. httpd-vhost.conf, 配置监听如上的端口处理:

复制代码
<VirtualHost *:49614>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "D:/xampp/htdocs"
    ServerName localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
    <Proxy /sjd-asset-mgt-api>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass /test-xxx-mgt-api http://10.40.10.159:49614/test-xxx-api
    ProxyPassReverse /test-xxx-mgt-api http://10.40.10.159:49614/test-xxx-api
</VirtualHost>
复制代码

 

3. 启动 httpd 服务即可。

如遇问题,查看 logs/error.log, 观察情况!

 

posted @   阿牛20  阅读(486)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2015-11-15 再次理解HTTP请求过程[概念原理篇]
点击右上角即可分享
微信分享提示