整理的一些常用到的 Nginx 配置

in Nginx with 0 comment

伪静态配置

Laravel

location / {
    try_files $uri $uri/ /index.php$is_args$query_string;
}

ThinkPhp

location / {
    if (!-e $request_filename){
        rewrite  ^(.*)$  /index.php?s=$1  last;   break;
    }
}

Vue History

location / {
  try_files $uri $uri/ /index.html;
}

设置代理【简单版】

location /document
{
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $http_host;
    proxy_set_header  X-Forwarded-Proto $scheme;
    #此处配置 程序的地址和端口号
    proxy_pass http://127.0.0.1:8181/;
}

设置 Nginx 访问日志

启动 nginx 时 会自动创建文件
access_log  /www/wwwlogs/host.com.log;
error_log  /www/wwwlogs/host-error.com.log;

设置 当前域名映射目录

root /www/wwwroot/host.com;

监听多端口

server
{
    listen 80;
    listen 8081;
    ...
}

设置域名地址

server_name host.com;

根据正则条件

被匹配域名 : http://host.com/TR1699043296-web.png
location ~ ^.*(TR.*)\.png$
{
    proxy_pass        http://127.0.0.1:9501;
    proxy_set_header  Host            $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
}

根据后缀条件

location ~ .*\.(js|css)?$
{
    # 过期时间
    expires      12h;

    # 开启日志
    error_log off;

    # /dev/null 忽略
    access_log /dev/null;
}

文件不存在的时候指向另外一个站点

始发站点

location ~ ^.*(TR.*)\.(png|pdf)$
{
  proxy_set_header  Host            $host;
  proxy_set_header  X-Real-IP        $remote_addr;
  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  if (!-e $request_filename) {
        proxy_pass http://127.0.0.1:9501;
  }
}

目标站点

location ~ ^.*(TR.*)\.(png|pdf)$
{
    root /www/wwwroot/project;
}

PDF 浏览器加载

if ($request_filename ~* ^.*(TR.*)\.pdf$){
    add_header Content-Disposition attachment;
}
Comments are closed.