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 reloadlocation
匹配优先级
完全匹配(=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;
}
}



