Nginx反向代理-端口轉發與目錄轉發


一、配置

nginx.conf中引入include vhost/*.conf;配置文件
在nginx.conf同級目錄下創建vhost/port.conf文件

二、端口轉發

server {
    listen 80;
    autoindex on;
    server_name www.port.com;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ) {
        return 404;
    }

    location / {
        proxy_pass http://127.0.0.1:8080;
        add_header Access-COntrol-Allow-Origin *;
    }
}

配置hosts
127.0.0.1 www.port.com
重啟Nginx
niginx -s reload
http://127.0.0.1:8080端口是配置的tomcat服務器
這樣當輸入www.port.com時就會轉發到tomcat服務器啦

二、文件轉發

server {
    listen 80;
    autoindex on;
    server_name www.file.com;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ) {
        return 404;
    }

    location / {
        root C:\file\img; # \是Windows /是Linux
        add_header Access-Control-Allow-Origin *;
    }
}

配置hosts
127.0.0.1 www.file.com
重啟Nginx
niginx -s reload
訪問 www.file.com 就可以看到C:\file\img下的圖片。


免責聲明!

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



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