请求转发
server {
listen 8088;
#server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://www.baidu.com;
}
}
说明:
- 通过浏览器请求 localhost:8088 地址,nginx匹配8088端口;
- 获取 location 下面的proxy_pass转发地址 ,转发请求至: http://www.baidu.com;
语法说明
语法规则: location [=|~|~*|^~] /uri/ { … }
= 精确匹配
^~ 以某个常规字符串开头
~ 区分大小写的正则匹配
~* 不区分大小写
/ 通用匹配,任何请求都会匹配到
配置文件
# 匹配用户
#user nobody;
# 生成进程数量
worker_processes 1;
# 生成日志存储路径
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 进程文件存储位置
#pid logs/nginx.pid;
events {
#设置网路连接序列化是否开启
accept_mutex on;
#设置一个进程是否同时接受多个网络连接,默认为off
multi_accept on;
#事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
#use epoll;
# 最多客户端连接数量
worker_connections 1024;
}
http {
# 文件类型支持conf/mime.types文件中的类型
include mime.types;
# 应用程序文件类型
default_type application/octet-stream;
# 日志输出格式
#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 logs/access.log main;
# 是否使用sendfile 方式传输文件
sendfile on;
# 数据包累计后发送
#tcp_nopush on;
#连接超时时间:单位秒(s)
keepalive_timeout 65;
# 开启数据打包
#gzip on;
server {
# 端口匹配
listen 8088;
# 请求地址匹配
server_name localhost;
# 网页编码
#charset koi8-r;
# 指定日至文件的路径及日志格式
#access_log logs/host.access.log main;
#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写
location / {
# 根目录
#root html;
# 默认欢迎页面
#index index.html index.htm;
# 拒绝的请求地址
deny 127.0.0.1;
# 允许的请求地址
allow 172.18.5.54;
}
# 404异常跳转页面
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
# 500 502 503 504 异常跳转页面
error_page 500 502 503 504 /50x.html;
# 50x.html异常页面根目录
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# 区分大小写匹配以.php结尾的请求
#location ~ \.php$ {
# 服务转发
#proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# PHP服务请求配置
#location ~ \.php$ {
# 项目根目录
#root html;
# 进程管理器服务通讯代理
#fastcgi_pass 127.0.0.1:9000;
# 默认访问页面
#fastcgi_index index.php;
# 脚本文件请求路径
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# 请求参数匹配fastcgi_params文件内容指向的变量
#include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# 拒绝的请求地址
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
# 将 8082 请求转发至 proxy_pass 指向的服务
server {
listen 8082;
#server_name localhost;
location / {
#root html;
#index index.html;
proxy_pass http://www.baidu.com;
}
# 请求路径:http://127.0.0.1:80/api/page/list
# 实际路径:http://127.0.0.1:8080/page/list
location /api {
proxy_pass http://127.0.0.1:8080/;
}
# 请求路径:http://127.0.0.1:80/api/page/list
# 实际路径:http://127.0.0.1:8080/page/list
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
# 请求路径:http://127.0.0.1:80/api/page/list
# 实际路径:http://127.0.0.1:8080/api/page/list
location /api/ {
proxy_pass http://127.0.0.1:8080;
}
}
# HTTPS server
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}