正反向代理参考
基本配置 nginx.conf
user nginx nginx; #nginx 进程运行的用户及用户组
worker_processes 8; #进程个数(推荐和cpu核数一致)
pid /run/nginx.pid; #pid文件路径
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
use epoll; #事件驱动模型 select|poll|kqueue|epoll
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
client_max_body_size 50m; #上传文件时大小限制,默认1m
server_tokens off; #隐藏nginx版本号
sendfile on; #允许sendfile高效传输模式传输文件,默认为off
tcp_nopush on; #将多个小包组成一个大包一起发送
tcp_nodelay on; #不等
gzip on; #开启gzip压缩
gzip_min_length 1k; #低于1kb的资源不压缩
gzip_buffers 4 16k; #缓存buffer
gzip_comp_level 5; #压缩比
gzip_proxied any; #无论header头部信息 都压缩
gzip_vary on; #代理时header头部添加Accept-Encoding
gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #压缩的文件类型
gzip_disable "MSIE [1-6]\."; #禁用IE6 gzip
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
include hosts/*.conf; #加载用户配置文件
}
用户配置文件 *.conf
server {
listen 81; #监听端口
server_name localhost; #域名
root /home/dev/al-api/public; #项目根目录
index index.html index.htm index.php server.php; #入口文件名称
# https配置
#listen 443 ssl;
#ssl_certificate /usr/local/nginx/cert/***.pem;
#ssl_certificate_key /usr/local/nginx/cert/***.key;
#ssl_session_timeout 5m;
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
#ssl_prefer_server_ciphers on;
access_log /data/logs/nginx/***/access.log; #请求日志路径
error_log /data/logs/nginx/common/error.log; #错误日志路径
location / { #所有请求转发php
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ { # php处理
include cors.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|ttf|woff|ico)$ # 静态资源文件
{
#root /data/dev/al-api/public/doc/preview;
expires 1d;
}
}
location (todo)
location / {} # 匹配所有
location = / {} # 只匹配 http://domain.com/
location ~ /abc {} # 只匹配 http://domain.com/abc
proxy
一般配置:
location /test/ {
proxy_set_header test test; # 自定义header:test
proxy_set_header Host $host;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://domain:8080;
}
proxy路径问题
1. 域名转发
简单来说就是proxy_pass只配置域名, 转发路径 = 真实请求路径
例如: http://localhost/test/xxx -> http://domain:8080/test/xxx
location /test/ {
proxy_pass http://domain:8080; # 不带路径(注意:'/'也是路径)
}
最终请求地址: http://domain:8080 + /test/xxx = http://domain:8080/test/xxx
2. 域名 + 路径转发
简单来说就是proxy_pass配置域名 + 路径, 转发路径 = proxy_pass + (真实请求路径 - 匹配规则)
例如1: http://localhost/test/xxx -> http://domain:8080/xxx
location /test/ {
proxy_pass http://localhost:8080/;
}
最终请求路径: http://domain:8080/ + (/test/xxx - /test/) = http://domain:8080/xxx
例如2: http://localhost/test/xxx -> http://domain:8080/demo/xxx
location /test/ {
proxy_pass http://localhost:8080/demo/;
}
最终请求路径: http://domain:8080/demo/ + (/test/xxx - /test/) = http://domain:8080/demo/xxx