Last active
April 8, 2024 07:29
-
-
Save J1nH4ng/287760abadab04f8438a77c05e823bc4 to your computer and use it in GitHub Desktop.
[Nginx 基本配置] Nginx 基本配置文件 #Nginx #Devops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nginx 跨域请求配置文件 | |
# 跨域请求的预检请求 | |
if ($request_method = 'OPTIONS') { | |
# * 允许所有来源的请求访问资源 | |
add_header Access-Control-Allow-Origin * always; | |
# 允许跨域请求的方法 | |
add_header Access-Control-Allow-Methods GET,POST,OPTIONS always; | |
# 允许跨域请求包含的头部信息 | |
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,X-Auth-Token,auth,authtoken,x-authtoken,Cache-Control,Content-Type,access-control-allow-origin,Authorization,X-Authorization,dept_id,deptid,x-terminal,x-preview-token,x-appid,x-nonce,X-signature,x-terminal,x-timestamp,ignorecanceltoken,x-debug-token,x-token' always; | |
# 指定预检请求可以被缓存的时间,单位为秒 | |
add_header 'Access-Control-Max-Age' 1728000 always; | |
# 设置响应内容的长度为 0,确保在返回状态码 204(No Content)的情况下,不会有实际的响应内容。 | |
add_header 'Content-Length' 0 always; | |
# 204 状态码,表示服务器成功处理,但是没有返回任何内容。(预检请求的常见响应) | |
return 204; | |
} | |
if ($request_method !~ ^(GET|POST)$) { | |
# 405 状态码,返回 "Method Not Allowed",方法不被允许。 | |
return 405; | |
} | |
add_header Access-Control-Allow-Origin * always; | |
add_header Access-Control-Allow-Methods GET,POST,OPTIONS always; | |
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,X-Auth-Token,auth,authtoken,x-authtoken,Cache-Control,Content-Type,access-control-allow-origin,Authorization,X-Authorization,dept_id,deptid,x-terminal,x-preview-token,x-appid,x-nonce,X-signature,x-terminal,x-timestamp,ignorecanceltoken,x-debug-token,x-token' always; | |
# 表示允许跨域请求携带凭证信息 | |
add_header 'Access-Control-Allow-Credentials' 'true' always; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nginx 本身的配置文件 | |
# | |
# 指定可以运行 nginx 服务的用户和用户组 | |
# user [user] [group]; | |
user nginx nginx; | |
# 指定工作线程数,可以制定具体的进程数,也可以使用自动模式 | |
worker_processes auto; | |
# 工作进程的 CPU 绑定由 Nginx 自动调整 | |
worker_cpu_affinity auto; | |
# 一个 Nginx 进程打开的最多文件描述符数量 | |
worker_rlimit_nofile 65535; | |
# 错误日志为,默认为 Nginx 的安装目录 | |
error_log /data/logs/nginx/error.log; | |
# 设置 pid 文件的存放位置 | |
pid /run/nginx.pid; | |
events { | |
# 事件模型,epoll 模型是 Linux 内核中的高性能网络 I/O 模型,如果跑在 FreeBSD 上面,就使用 kqueue 模型 | |
use epoll; | |
# 单个进程最大连接数 | |
worker_connections 65535; | |
# 如果请求数一直是维持在一个很高的水平,可以设置为 on | |
multi_accept on; | |
} | |
http { | |
# 文件拓展名与文件类型映射表 | |
include mime.types; | |
# 基于 Nginx_http_proxy_module 模块实现的代理功能,当 nginx 用做 client 服务器时使用 | |
include /usr/local/nginx/conf/proxy.conf; | |
# 当配置了 lua 模块时需要取消注释 | |
# lua_package_path "/usr/local/lua-resty-core-0.1/lib/lua/?.lua;/usr/local/lua-resty-lrucache-0.13/lib/lua/?.lua;;"; | |
# 默认文件类型 | |
default_type application/octet-stream; | |
# 默认编码 | |
charset utf-8; | |
# 开启高效文件传输模式 | |
sendfile on; | |
tcp_nopush on; | |
# 增加小包的数量,提高响应速度 | |
# 设置为 off 时会增加通信的延迟,提高带宽利用率 | |
tcp_nodelay on; | |
# 客户端请求头部的缓冲区大小 | |
cilent_header_buffer_size 4k; | |
# 大文件传输配置 | |
client_max_body_size 50m; | |
client_body_buffer_size 128k; | |
# 为打开文件指定缓存,默认是没有启用的 | |
# max 指定缓存数量,建议和打开文件数一致 | |
# inactive 是指经过多长时间文件没被请求后删除缓存 | |
# open_file_cache max=102400 inactive=20s; | |
# open_file_cache_valid 30s; | |
# open_file_cache_min_uses 1; | |
# 隐藏 nginx 版本号 | |
server_tokens off; | |
# 指定每个 TCP 连接可以保持多长时间 | |
keepalive_timeout 60s; | |
# 改善网站的性能:减少资源的占用,提高访问速度 | |
# nginx 接受 client 请求时的响应 | |
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; | |
# 开启 gzip 压缩 | |
gzip on; | |
gzip_min_length 1k; #最小压缩文件大小 | |
gzip_buffers 4 16k; #压缩缓冲区 | |
gzip_comp_level 2; #压缩等级 | |
gzip_types text/javascript text/css application/javascript application/json text/plain application/xml; #压缩类型 | |
gzip_vary on; #在响应头部添加 Accept-Encoding: gzip | |
log_format main_json | |
'{"@timestamp":"$time_iso8601",' #时间格式 | |
'"server_addr":"$server_addr",' #服务器端地址 | |
'"hostname":"$hostname",' #主机名 | |
'"ip":"$http_x_forwarded_for",' #浏览当前页面的用户计算机的网关 | |
'"remote_addr":"$remote_addr",' #浏览当前页面的用户计算机的ip地址(上一级ip) | |
'"request":"$request",' #客户端的请求地址 | |
'"request_method":"$request_method",' #http请求方法 | |
'"scheme":"$scheme",' #请求使用的web协议 | |
'"body_bytes_sent":"$body_bytes_sent",' #传输给客户端的字节数(不算响应头) | |
'"request_time":"$request_time",' #处理客户端请求使用的时间 | |
'"upstream_response_time":"$upstream_response_time",' #请求过程中 upstream 响应时间 | |
'"upstream_addr":"$upstream_addr",' #后台 upstream 地址,即真正提供服务的主机地址 | |
'"host":"$host",' #请求地址 | |
'"uri":"$uri",' #请求中的当前url | |
'"request_uri":"$request_uri",' #请求原始url | |
'"args":"$args",' #请求中的参数值 | |
'"http_referer":"$http_referer",' #url 跳转来源,用来记录从那个页面链接访问过来的 | |
'"http_user_agent":"$http_user_agent",' #用户终端浏览器等信息 | |
'"status":"$status"}'; #http响应代码 | |
# nginx 访问日志 | |
access_log /data/logs/nginx/access.log main_json; | |
include /usr/local/nginx/conf/conf.d/*.conf; | |
include /usr/local/nginx/conf/conf.d/*/*.conf; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nginx 外层代理服务器配置文件模板 | |
server { | |
# 监听 443 端口,启用 SSL | |
listen 443 ssl; | |
# 设置域名 | |
server_name test.14bytes.com; | |
# SSL 证书文件路径 | |
ssl_certificate /usr/local/nginx/cert/test.14bytes.com.pem; | |
# SSL 证书密钥文件存放路径 | |
ssl_certificate_key /usr/local/nginx/cert/test.14bytes.com.key; | |
# SSL 超时时间 | |
ssl_session_timeout 5m; | |
# 指定支持的 SSL/TLS 协议 | |
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; | |
# 指定允许的加密算法 | |
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; | |
# 优先使用服务器指定的加密算法顺序 | |
ssl_prefer_server_ciphers on; | |
# 请求的 URL | |
location / { | |
# 将请求代理到指定的后端服务器,不加端口,默认请求 80 端口 | |
proxy_pass http://192.168.8.8:8888; | |
# 设置哈希表的最大大小 | |
proxy_headers_hash_max_size 51200; | |
# 设置哈希的桶大小 | |
proxy_headers_hash_bucket_size 6400; | |
# 设置 Host 头部为原始值 | |
proxy_set_header Host $host; | |
# 转发真实客户端的 IP 地址 | |
proxy_set_header X-Real-IP $remote_addr; | |
# 转发原始客户端的 IP 地址 | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
} | |
# 包含额外的配置,可能包含拒绝特定的请求或 IP 地址 | |
include /usr/local/nginx/conf/conf.d/extras/deny.conf; | |
# 跨域请求配置文件 | |
include /usr/local/nginx/conf/conf.d/extras/cors.conf; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set header | |
proxy_set_header Host $host; #设置真实客户端地址 | |
proxy_set_header X-Real-IP $remote_addr; #设置客户端真实IP地址 | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #记录代理地址 | |
# timeout | |
proxy_connect_timeout 30; #后端服务器连接的超时时间(这个超时不能超过75秒) | |
proxy_send_timeout 60; #发送请求给upstream服务器的超时时间(默认为60s) | |
proxy_read_timeout 60; #设置从被代理服务器读取应答内容的超时时间(默认为60s) | |
# buffer | |
proxy_buffering on; | |
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 | |
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置 | |
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) | |
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 | |
# next_upstream | |
proxy_next_upstream error timeout invalid_header http_502 http_504; #设置重试的场景(默认值为 error 和 timeout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment