nginx虚拟主机配置项

50x和40x配置

error_page 500 502 503 504 /50x.html;
location = /50x.html {
  root html;
}

error_page 404 /404.html;
location = /404.html {
  root html;
}

URL重写配置(例如隐藏)

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

  index index.html;
  try_files $uri $uri/ /index.html last;
}

禁止访问文件或目录配置

location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) {
    return 404;
}

禁止脚本执行配置

location ~ ^/(uploads|assets)/.*\.(php|php5|jsp)$ {
  deny all;
}

请求此类文件时进行下载而不是预览

location ~ .*\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$ {
  add_header Content-Disposition attachment;
}

缓存js和css配置

location ~ .*\.(js|css)?$ {
  expires 12h;
  error_log /dev/null;
  access_log /dev/null;
}

缓存图片配置

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
  expires 30d;
  error_log /dev/null;
  access_log /dev/null;
}

防盗链配置

location ~ .*\.(jpg|jpeg|gif|png|js|css)$ {
  expires 30d;
  access_log /dev/null;
  valid_referers asynctp.com;
  if ($invalid_referer){
    return 404;
  }
}

php配置(unix domain socket方式)

location ~ [^/]\.php(/|$) {
  root root;
  #try_files $uri =404;
  fastcgi_index index.php;
  fastcgi_pass unix:/tmp/php/var/run/php-fpm.sock;
  #pathinfo给fastcgi权限 可支持?s=/module/controller/action的url访问模式
  fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param PATH_INFO $fastcgi_path_info;
  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  include fastcgi_params;
}

跨域CORS配置

location / {
  #可接受来源
  add_header 'Access-Control-Allow-Origin' '*' always;
  #跨域支持的请求类型 ',OPTIONS,GET,POST,PUT,DELETE'
  add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS' always;
  #是否允许发送Cookie
  add_header 'Access-Control-Allow-Credentials' 'true' always;
  #跨域请求允许Header头可添加多个 如If-Modified-Since
  add_header 'Access-Control-Allow-Headers' 'If-Modified-Since' always;
  #缓存时间(秒)
  add_header 'Access-Control-Max-Age' '1728000' always;
  #预检请求处理
  if ($request_method = 'OPTIONS') {
    add_header 'Content-Length' '0';
    return 204;
  }
}

SSl配置

server {
  listen 80 443 ssl;
  http2 on;
  server_name _;

  #强制https
  if ($server_port !~ 443) {
    rewrite ^(/.*)$ https://$host$1 permanent;
  }
  ssl_certificate vhost/cert/server_name.pem;
  ssl_certificate_key vhost/cert/server_name.key;
  ssl_protocols TLSv1.3;
  ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;
  ssl_session_timeout 10m;
  add_header Strict-Transport-Security "max-age=31536000";
  error_page 497 https://$host$request_uri;
}

相关推荐

  1. nginx虚拟主机配置

    2024-07-22 14:48:07       17 阅读
  2. Nginx虚拟主机配置

    2024-07-22 14:48:07       49 阅读
  3. Apache虚拟主机VirtualHost配置详解

    2024-07-22 14:48:07       15 阅读
  4. Nginx三种虚拟主机配置

    2024-07-22 14:48:07       43 阅读
  5. nginx 基于IP的多虚拟主机配置

    2024-07-22 14:48:07       40 阅读
  6. 部署nginx虚拟主机及SSL虚拟主机

    2024-07-22 14:48:07       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 14:48:07       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 14:48:07       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 14:48:07       45 阅读
  4. Python语言-面向对象

    2024-07-22 14:48:07       55 阅读

热门阅读

  1. 在 CentOS 7 上编译安装 Python 3.11

    2024-07-22 14:48:07       14 阅读
  2. 算法学习3——搜索算法

    2024-07-22 14:48:07       17 阅读
  3. IaaS是什么的简称?关于IaaS的介绍

    2024-07-22 14:48:07       18 阅读
  4. [C++]——常见内存泄漏场景

    2024-07-22 14:48:07       16 阅读
  5. element表单disabled功能失效问题

    2024-07-22 14:48:07       16 阅读
  6. 塔子哥的浏览记录-小红书2024笔试(codefun2000)

    2024-07-22 14:48:07       21 阅读
  7. [算法题]mari和shiny

    2024-07-22 14:48:07       17 阅读
  8. 面试官:你对ConcurrentHashMap了解多少?

    2024-07-22 14:48:07       16 阅读
  9. 封装的通用链表(list.c/list.h/test_list.c)

    2024-07-22 14:48:07       17 阅读
  10. 将SQL中的占位符替换成参数

    2024-07-22 14:48:07       14 阅读