Nginx 优化 -04

Nginx优化

一、跨域访问
1.盗链和跨域的区别
盗链是由盗链的网站向被盗链的网站发起get请求获取内容

跨域是由跨域的网站向被跨域的网站发起一个完整http请求,甚至是完全跳转
2.配置被跨域的网站
[root@web01 conf.d]# vim linux12.beikuayu.com.conf
server {
    listen 80;
    server_name linux12.beikuayu.com;

    location / {
        root /mm;
        index index.html;
    }   
}

#配置站点
[root@web01 mm]# cat index.html 

2021被跨越网站!

## 授权
[root@web01 ~]# chown -R www.www /mm/

## nginx -t检查并重启
[root@web01 ~]# systemctl restart nginx

# 配置本地hosts
10.10.0.7  linux12.beikuayu.com
3.配置跨域网站
[root@lb01 conf.d]# vim linux12.kuayu.com.conf 
server {
    listen 80;
    server_name linux12.kuayu.com;

    location ~* / {
        root /mm;
        index index.html;
    }
}

#配置跨域的站点文件
[root@lb01 conf.d]# vim /mm/index.html 
<html lang="en">
<head>
        <meta charset="UTF-8" />
        <title>测试ajax和跨域访问</title>
        <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
        $.ajax({
        type: "GET",
        url: "http://linux12.beikuayu.com",
        success: function(data) {
                alert("sucess 哎哟 不错 成功了!!!");
        },
        error: function() {
                alert("fail!!,我靠,TM的不让进去啊,走啦!");
        }
        });
});
</script>
        <body>
                <h1>测试跨域访问啊</h1>
        </body>
</html>

#配置站点
[root@ lb01 mm]# cat index.html 

2021被跨越网站!

## 授权
[root@ lb01 ~]# chown -R www.www /mm/

## nginx -t检查并重启
[root@ lb01 ~]# systemctl restart nginx

# 配置本地hosts
10.10.0.5  linux12.kuayu.com
4.配置本地hosts测试
#windows的hosts

10.10.0.5 linux12.kuayu.com
10.10.0.7 linux12.beikuayu.com

[root@web01 conf.d]# vim /etc/hosts
10.10.0.5 linux12.kuayu.com
10.10.0.7 linux12.beikuayu.com

[root@lb01 conf.d]# vim /etc/hosts
10.10.0.5 linux12.kuayu.com
10.10.0.7 linux12.beikuayu.com

# 5.测试跨域访问

http://linux12.kuayu.com/

http://linux12.beikuayu.com/
6.配置允许被跨域
[root@web01 conf.d]# vim linux12.beikuayu.com.conf 
server {
    listen 80;
    server_name linux12.beikuayu.com;

    location / {
        root /mm;
        index index.html;
        #允许跨域的网站
        add_header Access-Control-Allow-Origin *;
        #允许跨域网站发起的请求类型
        add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS';
    }
}
二、nginx的CPU亲和
1.查看cpu状态
## 首先要根据的电脑配置查看自己多少CPU!

[root@web01 ~]# lscpu
CPU(s):                2    #cpu数
On-line CPU(s) list:   0-1  #cpu核心数
Thread(s) per core:    1    #1块cpu
Core(s) per socket:    1
NUMA node0 CPU(s):     0-1
2.修改nginx配置
[root@web01 ~]# vim /etc/nginx/nginx.conf 
worker_processes  2;

[root@web01 ~]# systemctl restart nginx
3.没有配置亲和的情况
[root@web01 conf.d]# ps -eo pid,args,psr | grep [n]ginx
  1869 nginx: master process /usr/   1
  1870 nginx: worker process         1  #两个cpu都在抢占1的cpu,默认从0开始。
4.配置cpu亲和
# 1.方式1
worker_processes    2;
worker_cpu_affinity 0001 0010 ;

worker_processes    8;
worker_cpu_affinity 00000001 00000010 ...;

# 2.方式2
worker_processes    2;
worker_cpu_affinity 0101 1010;
  
# 3.方式3
'表示'`nginx`'里的每个'`worker`'自动平均分配CPU。
worker_processes auto;
worker_cpu_affinity auto;

[root@web01 ~]# vim/etc/nginx/nginx.conf
---------------------------- `核心模块` ---------------------------
user  www; #启动此进程的用户   # 增加下面两行
worker_processes  auto;   	# master给worker分配工作,worker的数量,与CPU的数量相关
worker_cpu_affinity auto; 	# 自动平均分配CPU。
5.配置CPU亲和后
[root@web01 conf.d]# ps -eo pid,args,psr | grep [n]ginx
  2070 nginx: master process /usr/   1
  2071 nginx: worker process         0
  2072 nginx: worker process         1
三、nginx通用优化文件
1.通用优化配置
[root@web01 conf.d]# cat /etc/nginx/nginx.conf

user www;									#nginx启动用户
worker_processes auto;						 #nginx工作进程数
worker_cpu_affinity auto;					  #开启CPU亲和
error_log /var/log/nginx/error.log warn;	  #错误日志,存放路径,记录日志的级别
pid /run/nginx.pid;							#指定pid文件位置
worker_rlimit_nofile 1024;					 #指定nginx服务的最大打开文件数

events {
    use epoll;								 #使用epoll网络模型
    worker_connections 10240;				 #worker工作进程的最大连接数
}

http {
    include             mime.types;				    #nginx能识别的文件类型
    default_type        application/octet-stream;   #nginx不识别的文件类型默认下载

   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" 
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';		#配置日志格式
    charset utf-8;								    #指定字符
    access_log  /var/log/nginx/access.log  main		#指定访问日志路径,调用日志的格式
    server_tokens off;							    #隐藏版本号
    client_max_body_size 500m;					    #上传文件大小限制
    sendfile            on;						    #高效读取
    tcp_nopush          on;							#高效传输
    #tcp_nodelay         on;						#实时传输
    keepalive_timeout   65;							#开启长连接
    gzip on;									    #开启压缩
    gzip_disable "MSIE [1-6]\.";					#指定不压缩的浏览器
    gzip_http_version 1.1;							#压缩后传输的协议
    gzip_comp_level 5;								#压缩的级别
    gzip_buffers 16 10k;								#压缩缓存
    gzip_min_length 1024;							#开启压缩的最小值
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg;		#压缩的文件类型
    include /etc/nginx/conf.d/*.conf;				#包含的配置文件
}
2.nginx优化总结
1、CPU亲和、worker进程数、调整nginx进程打开的文件句柄数
2、使用Epool网络模型、调整每个worker进程的最大连接数
3、文件的高效读取sendfile、nopush
4、文件的传输实时性、nodealy
5、开启tcp长连接,以及长连接超时时间keepalive_timeout
6、开启文件传输压缩gzip
7、开启静态文件expires缓存
8、隐藏nginx版本号
9、禁止通过ip地址访问,禁止恶意域名解析,只允许域名访问
10、配置防盗链、以及跨域访问
11、防DDOS、cc攻击,限制单IP并发连接,以及http请求
12、优雅显示nginx错误页面
13、nginx加密传输https优化
14、nginx proxy_cache、fastcgi_cache、uwsgi_cache 代理缓存,第三方工具(squid、varnish)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FikL-09-19

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值