nginx

1、nginx.conf配置
1.1、作服务器用

    server {
        # 监听80端口和localhost域名(对域名对应的ip也生效)
        listen       80;
        server_name  localhost;

        # 匹配 /开头 的路径(例如 http:localhost/aaa)
        location / {
            # 资源查找路径(http:localhost/aaa.html 匹配 html/aaa.html)
            root   html;
            # 路径首页(http:localhost/aaa.html 匹配 html/index.html html/index.htm)
            index  index.html index.htm;
            # 请求路径不存在的情况(http:localhost/假设不存在.html 匹配 html/index.html)
            try_files $uri $uri/ /index.html;
        }

        # 匹配 /t开头 的路径(例如 http:localhost/t http:localhost/t/aaa)
        location /t {
            # 资源查找路径(http:localhost/t/aaa.html 匹配 html/test/t/aaa.html)
            root   html/test;
            # 路径首页(http:localhost/t 匹配 html/test/t/index.html html/test/t/index.htm)
            index  index.html index.htm;
        }

        # 匹配 /t2开头 的路径(例如 http:localhost/t2 http:localhost/t2/aaa)
        location /t2 {
            # 资源查找路径(http:localhost/t2/aaa.html 匹配 html/test/aaa.html)
            alias   html/test;
            # 路径首页(http:localhost/t2 匹配 html/test/index.html html/test/index.htm)
            index  index.html index.htm;
        }

        # 匹配 /e 的路径(例如 http:localhost/e http:localhost/e/ 不匹配 http:localhost/e/aaa)
        location = /e {
            root   html/exc;
            # 路径首页(http:localhost/e 匹配 html/exc/e/index.html html/exc/e/index.htm)
            index  index.html index.htm;
        }

        # 当服务器返回如下响应码 会返回 /50x.html页面
        error_page   500 502 503 504  /50x.html;

        # 匹配 /50x.html 的路径(例如 http:localhost/50x.html)
        location = /50x.html {
            # 资源查找路径(http:localhost/50x.html 匹配 html/50x.html)
            root   html;
        }
    }

1.2、作反向代理用

    # 声明后端服务的名称为myhdserver并指定后端真实域名和端口
    upstream myhdserver {
        server www.hd.com:8080;
    }

    server {
        # 监听80端口和localhost域名(对域名对应的ip也生效)
        listen       80;
        server_name  localhost;

        # 匹配 /api/开头 的路径(例如 http:localhost/api/aaa)
        location /api/ {
            # 转发路径(例如http:localhost/api/aaa 转发为 http:www.hd.com:8080/api/aaa)
            proxy_pass http://myhdserver;  # 转发到后端服务器
            # 域名
            proxy_set_header Host $host;
            # 浏览器ip
            proxy_set_header X-Real-IP $remote_addr;
            # 浏览器ip,代理器ip  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # 协议 http https
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        # 匹配 /api/开头 的路径(例如 http:localhost/api/aaa)
        location /api/ {
            # 转发路径(例如http:localhost/api/aaa 转发为 http:www.hd.com:8080/aaa)
            proxy_pass http://myhdserver/;  # 转发到后端服务器
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }

1.3、注意点

            location /t      location /t/
/t/            匹配              匹配 
/t/aaa.html    匹配              匹配
/t             匹配             不匹配
/tabc          匹配             不匹配
/t-xxx         匹配             不匹配
posted @ 2026-01-29 20:31  略乏旅人  阅读(1)  评论(0)    收藏  举报