nginx配置反向代理转发

Posted jongty

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx配置反向代理转发相关的知识,希望对你有一定的参考价值。

环境:Windows10、PHPstudy2018(nginx+mysql5.6+php7.2)

这里直接贴上nginx.conf配置文件信息。其实主要就是server的修改。把对应的路径改了就行(server也可以单独包含在vhosts.conf文件中)

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 128k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 32k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    gzip_disable "MSIE [1-6].";

    server_names_hash_bucket_size 128;
    client_max_body_size     100m;
    client_header_buffer_size 256k;
    large_client_header_buffers 4 256k;

    server {
        listen       80;                                #前端访问的端口
        server_name  192.168.1.28;                      #前端访问的虚拟域名
        root    "E:/phpstudy/PHPTutorial/WWW/front";    #打包后的前端目录(这里是vue项目目录
        location / {                                    #必需(/下面访问***
           try_files $uri $uri/ /index.html;
        }
        location ^~ /api/ {                             #匹配到/api/的话转发到8085端口
            proxy_pass  http://erp.test.com:8085;
            proxy_set_header X-Forwarded-For  $remote_addr;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        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;
        }
    }

    server {
        listen       8085;                                      #后台api端口
        server_name  erp.test.com;                              #后台虚拟域名
        root    "E:/phpstudy/PHPTutorial/WWW/tp/public";        #api目录
        location / {
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
            }
            try_files $uri $uri/ =404;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        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;
        }
    }

### host文件添加对应域名
### 127.0.0.1 localhost
### 192.168.1.28   erp.test.com
### 配置成功后自己的机器上可以使用localhost、erp.test.com、ip、127.0.0.1访问项目(8080)
### 局域网内别人的机器使用这台机器的ipv4地址访问(8080)


include vhosts.conf;

}

 

 

以上是关于nginx配置反向代理转发的主要内容,如果未能解决你的问题,请参考以下文章

Nginx反向代理2--配置文件配置

nginx配置 - 反向代理,转发配置

Nginx转发+反向代理实现跨域接口转发

nginx配置反向路由代理

nginx配置反向路由代理

nginx反向代理配置