700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > nginx 配置文件 linux Linux-nginx.conf配置文件模板

nginx 配置文件 linux Linux-nginx.conf配置文件模板

时间:2024-05-21 18:30:14

相关推荐

nginx 配置文件 linux Linux-nginx.conf配置文件模板

user nginx; #修改nginx服务的运行用户为nginx,此用户提前在系统创建

worker_processes 4; #nginx运行进程的个数设置为4

worker_cpu_affinity 0001 0010 0100 1000; #设置CPU亲和力,此处为四核四线程

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

worker_rlimit_nofile 102400; #最多可打开的文件数,为102400个

events {

use epoll; #使用异步IO模型

worker_connections 102400; #单个进程允许客户端最大连接并发数为102400个

}

http {

include mime.types; #包含的媒体类型

default_type application/octet-stream; #默认的媒体类型

#控制单个IP或者域名的访问次数

limit_conn_zone $binary_remote_addr zone=addr:1m; #下接server{}的第86行

#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘

# ‘$status $body_bytes_sent “$http_referer” ‘

# ‘”$http_user_agent” “$http_x_forwarded_for”‘;

#access_log logs/access.log main;

sendfile on; #开启高效文件传输模式

#tcp_nopush on; #必须在sendfile开启模式才有效,防止网络堵塞,减少网络报文段的数量

#连接时间调优:

#keepalive_timeout 0;

keepalive_timeout 65; #连接超时时间

tcp_nodelay on; #防止网络阻塞,需要包含在keepalived参数中才有效

client_header_timeout 15; #客户端请求头读取时间

client_body_timeout 15; #客户端请求主体时间

send_timeout 15; #响应客户端超时时间

client_max_body_size 10m; #设置文件上传的大小限制为10m

#gzip调优:

gzip on; #开启压缩功能

gzip_min_length 1k; #设置允许压缩的页面最小字节数

gzip_buffers 4 32k; #压缩缓冲区大小

gzip_http_version 1.1; #压缩版本,默认为1.1

gzip_comp_level 9; #压缩比例,用来指定GZIP压缩比,1压缩比最小,处理速度最快,9压缩比最大,传输速度快,但是处理慢,也比较消耗CPU资源。

gzip_types text/css text/xml application/javascript; #用来指定压缩的类型

gzip_vary on; #vary header支持,该选项可以让前端的缓存服务器缓存经过GZIP压缩的页面

fastcgi_connect_timeout 300; #指定链接到后端FastCGI的超时时间

fastcgi_send_timeout 300; #向FastCGI传送请求的超时时间

fastcgi_read_timeout 300; #指定接收FastCGI应答的超时时间

fastcgi_buffer_size 64k; #指定读取FastCGI应答第一部分需要用多大的缓冲区

fastcgi_buffers 4 64k; #指定本地需要多少和多大的缓冲区来缓冲FastCGI的应答请求

fastcgi_busy_buffers_size 128k; #建议设置为fastcgi_buffer的两倍,繁忙时候的buffer

fastcgi_temp_file_write_size 128k; #在写入fastcgi_temp_path时将用多大的数据库

#fastcgi_temp_path /data/ngx_fcgi_tmp;

fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 #缓存路径,levels目录层次2级

keys_zone=ngx_fcgi_cache:512m #定义了一个存储区域名字,缓存大小

inactive=1d max_size=40g; #不活动的数据在缓存中多长时间,目录总大小

server {

#IP和301优化:

#使客户端无法使用IP地址访问网站:

listen 80 default_server; #监听端口为80

server_name ; #域名设置

if ($host = ‘‘ ) {

rewrite ^/(.*)$ /$1 permanent;

} #将对的访问跳转到

#方法一:跳转到其他站点

#rewrite ^ $request_uri?; #使用IP访问时跳转到

#方法二:403反馈:

#return 403;

charset utf-8; #指定网站的编码集为utf-8

#access_log logs/host.access.log main;

location / {

root html;

index index.html index.htm;

limit_conn addr 1; #上接http{}的第23行

}

#内部身份验证:

location /ccc/ {

auth_basic “Welcome to Road to Death”; #提示信息

auth_basic_user_file /usr/local/nginx/conf/passwd; #账号密码文件

}

#FastCGI调优:

location ~ .*\.(php|php5)?$

{

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

fastcgi_cache ngx_fcgi_cache;

fastcgi_cache_valid 200 302 1h;

fastcgi_cache_valid 301 1d;

fastcgi_cache_valid any 1m;

fastcgi_cache_min_uses 1;

fastcgi_cache_use_stale error timeout invalid_header http_500;

fastcgi_cache_key http://$host$request_uri;

}

#expires缓存调优:

#匹配以.gif|jpg|jpeg|png|bmp|swf结尾的文件,缓存,以.js|css结尾的文件缓存30天

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 3650d;

}

location ~ .*\.(js|css)?$

{

expires 30d;

}

#对目录进行判断

#对images|javascript|js|css|flash|media|static等目录下的内存缓存360天,对文件robot.txt缓存7天

location ~ ^/(images|javascript|js|css|flash|media|static)/ {

expires 360d;

}

location ~(robots.txt) {

expires 7d;

break;

}

#目录文件访问控制

#禁止访问images目录下的可执行文件程序文件:

location ~ ^/images/.*\.(php|php5|.sh|.py|.pl)$ {

deny all;

}

#多目录组合配置方法:

#拒绝访问images目录下的attachment和avatar目录下的所有可执行文件:

location ~ ^/images/(attachment|avatar)/.*\.(php|php5|.sh|.py|.py)$ {

deny all;

}

#禁止访问*.txt文件

# location ~* \.(txt|doc)$ {

# if ( -f $request_filename) {

# root /usr/local/nginx/html;

# break;

# }

# deny all;

# }

#重定向到某个URL

location ~* \.(txt|doc)$ {

if ( -f $request_filename) {

root /usr/local/nginx/html;

rewrite ^/(.*)$ last; #对应的URL路径

break;

}

}

#对指定目录进行限制:

location /aaa/ { return 404 ; }

location /bbb/ { return 403 ; }

#上述等同于:

location ~ ^/(aaa|bbb)/ {

deny all;

}

#来源访问控制:

#允许192.168.1.0/24网站下的aaa目录,拒绝所有

location ~ ^/(aaa)/ {

allow 192.168.1.0/24;

deny all;

}

#允许192.168.1.0/24网段访问网站,拒绝所有,针对整个网站如下:

#location / {

# allow 192.168.1.0/24; #此处可使用精确匹配,也可使用正则

# deny all;

#}

#也可以通过if语句控制,给以友好的错误提示:

if ( $remote_addr = 192.168.1.38 ) {

return 404;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files, if Apache‘s document root

# concurs with nginx‘s one

#

#location ~ /\.ht {

# deny all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

# listen 8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location / {

# root html;

# index index.html index.htm;

# }

#}

# HTTPS server

#

#server {

# listen 443 ssl;

# server_name localhost;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;

# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;

# ssl_prefer_server_ciphers on;

# location / {

# root html;

# index index.html index.htm;

# }

#}

}

原文链接:/Vampire-MIn/p/13084954.html

本文来自网络,不代表手讯网立场。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。