http {
# .........省略部分默认配置
# 字符集配置
charset utf-8;
# 包压缩配置
# 开启gzip
gzip on;
# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
gzip_comp_level 5;
# 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
# 禁用IE 6 gzip
gzip_disable "MSIE [1-6]\.";
# 重定向配置(例子)
server {
listen 80;
server_name abc.com;
rewrite ^/(.*) http://www.abc.com/$1 permanent;
}
# 站点配置(例子)
server {
listen 80;
server_name www.abc.com; # 主机名
root /data/cbg.com/public; # 项目index.php所在目录
index index.php index.html index.htm;
# 接口转发(例子)
location /api { # 我测试是,location最后结尾带'/'就是301,不带'/'就是直接转发包。
# 例:abc.com/api/user/info 会转发到 http://api.com/admin/user/info
# 当proxy_pass的地址带path时,就是把匹配出来的path截取掉(/api),把后面的path(/user/info)附加到proxy_pass的地址最后。
# 当proxy_pass不带path也不带'/',就会把所有path加到域名后面。
proxy_pass http://api.com/admin;
# 请求头,配置格式为 proxy_set_header 键 值
proxy_set_header Host $proxy_host; # 跳转的主机名,默认是域名。可自定义:例如这里定义值为abc,相应的添加abc到http://api.com/admin站点的server_name中,即可识别。
proxy_set_header X-Real-IP $remote_addr; # 远程IP地址。为了主机可获取到客户端的IP地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
# 本例使用laravel的rewrite,如果是其他框架,请使用相应配置。
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
fastcgi_pass 172.18.1.11:9000; # php容器的IP
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}
}
nginx http配置说明,逐渐完善。
于 2021-05-18 12:17:23 首次发布