nginx 配置文件 linux,Linux-nginx.conf配置文件模板
发布日期:2021-06-24 12:56:42 浏览次数:2 分类:技术文章

本文共 5613 字,大约阅读时间需要 18 分钟。

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  www.bdqn.com bdqn.com;        #域名设置

if ($host = ‘bdqn.com‘ ) {

rewrite ^/(.*)$ http://www.bdqn.com/$1 permanent;

}    #将对bdqn.com的访问跳转到www.bdqn.com

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

#rewrite ^ http://www.baidu.com$request_uri?;    #使用IP访问时跳转到www.baidu.com

#方法二: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结尾的文件,缓存10年,以.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 ^/(.*)$ http://www.baidu.com 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;

#    }

#}

}

原文链接:https://www.cnblogs.com/Vampire-MIn/p/13084954.html

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

转载地址:https://blog.csdn.net/weixin_32862641/article/details/116890046 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:arm-linux-ld: cannot find -lgcc,/arm-linux-gnueabi/bin/ld: cannot find crt1.o
下一篇:linux虚拟机换行编写错误,windows下linux虚拟机oracle处理换行符的奇怪之处

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月31日 05时25分38秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

app运行提示Unable to Initialize Unity Engine 2019-04-27
spring boot 与 Ant Design of Vue 实现修改按钮(十七) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除按钮(十八) 2019-04-27
spring boot 与 Ant Design of Vue 实现角色管理布局以及角色的列表(十九) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增角色(二十) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改角色(二十一) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除角色(补二十一) 2019-04-27
spring boot 与 Ant Design of Vue 实现组织管理布局的实现(二十二) 2019-04-27
spring boot 与 Ant Design of Vue 实现左侧组织树(二十三) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增组织(二十四) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改组织(二十五) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除组织(二十六) 2019-04-27
spring boot 与 Ant Design of Vue 实现获取用户列表(二十七) 2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二) 2019-04-27
什么是服务熔断? 2019-04-27
服务器压力过大?CPU打满?我来帮你快速检查Linux服务器性能 2019-04-27
C++面经总结之《Effective C++》(一) 2019-04-27
C++面经总结之《Effective C++》(二) 2019-04-27
这是什么“虎狼之词”啊!!!程序员的健康问题,看一线老中医怎么说!!! 2019-04-27
打开我的收藏夹 -- Python数据分析杂谈 2019-04-27