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;

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM