栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Nginx

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nginx

Nginx 安装nginx
yum install -y nginx
启动nginx
systemctl start nginx
systemctl stop nginx
systemctl reload nginx

taskkill /f /t /im nginx.exe
start nginx
stop nginx
nginx -s reload
location
匹配优先级
	完全匹配(=uri) > 优先前缀匹配(=^~) > 正则匹配(~*) > 前缀匹配

指定前缀
location /greet {
    
}

完全匹配,大小写不敏感
location = /greet {
    
}

正则匹配,大小写敏感
location ~ /greet {
    
}

正则匹配,大小写不敏感
location ~* /greet {
    
}

优先前缀匹配,大小写敏感
location ^~ /greet {
    
}
include

test1.com.conf

server {
        listen 8000;
        server_name test1.com;
        location / {
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # proxy_pass http://xxx.xxx.xxx;
            echo "test1.com";    # 输出测试

        }

}

nginx.conf

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    
    include /test1.com.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
反向代理
server {
        listen       80;
        server_name  localhost;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}
server {
    listen       8088;
    server_name  localhost;
    location /api {
        proxy_pass http://127.0.0.1:5000;
    }
    location / {
       proxy_pass http://127.0.0.1:6000;
    }
}

假设下面四种情况分别用 http://127.0.0.1:8088/proxy/test.html 进行访问
proxy_pass以/结尾,不包含location
proxy_pass后没有/,包含location

server {
    listen       8088;
    server_name  localhost;
    location /proxy {
        proxy_pass http://127.0.0.1:5000;
        访问:http://127.0.0.1:5000/proxy
    }
    location /proxy/ {
        proxy_pass http://127.0.0.1:5000;
        访问:http://127.0.0.1:5000/proxy/
    }
    location /proxy/ {
        proxy_pass http://127.0.0.1/;
        访问:http://127.0.0.1:5000/test.html
    }
    location /proxy/ {
        proxy_pass http://127.0.0.1;
        访问:http://127.0.0.1:5000/proxy/test.html
    }
    location /proxy/ {
        proxy_pass http://127.0.0.1/aaa/;
        访问:http://127.0.0.1:5000/aaa/test.html
    }
    location /proxy/ {
        proxy_pass http://127.0.0.1/aaa;
        访问:http://127.0.0.1:5000/aaatest.html
    }
    location / {
       proxy_pass http://127.0.0.1:3000;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号