nginx配置说明

一:server配置

参考Nginx

1.root:匹配时:root定义的路径+port后面的路径
http://ww.xxx.com/img/xjj.png
location ^~ /img {
    root /var/www/static/;
}

最终:会找到/var/www/static/img/xjj.png

2.alias:匹配是:alias定义的路径+location匹配部分后面的路径
http://www.xxx.com/img/xjj.png
location ^~ /img {
    alias /var/www/static/images/;
}

最终:会找到 /var/www/static/images/xjj.png

3.upstream
upstream back_server {
    server backend1.example.com weight=5;
    server 127.0.0.1:8001 max_fials=3 fail_timeout=5;  
    server unix:///data/sc_api/sc_api/my_sock.sock;

}

参考nginx反向代理及参数配置

server:指定一台上游服务器的名字,可以是(域名/ip地址端口/UNIC句柄)

weight:设置权重

max_fail:在fail_timeout时间段内,转发失败次数超过number就不可用

fail_timeout:设置超时时间

down:表示所有的上游服务器永久下线,只在使用ip_hash配置项时才有用。

backup:在使用ip_hash配置项时它是无效的。表示所在的上游服务器只是备份服务器,只有在所有的非备份上游服务器都失效后,才会向所在的上游服务器转发请求。

proxy_pass:http:/ip:port/uri 或者 back_server

proxy_set_header Host $host; # 反向代理是不会转发请求中的Host头部的,如需转发需加上配置

proxy_redirect http://localhost:8000/two/ http://frontendone; # 重定向 location字段的URL是http://localhost:8000/two/some/uri/ 转发给客户端的location就是:http://frontendonesome/uri/

4.server配置demo
server {
    listern 8000;
    server_name www.centyuan.top centyuan.top;
    charset utf-8;  # 字符集
    access_log  /data/sc_api/logs/my_access.log;
    error_log /data/sc_api/logs/my_error.log;
    client_max_body_size 75M  # 上传最大限制
    loction / {
        root /data/dist;
        index index.html
        try_files $uri $uri/ /index.html;
        # 1.用户请 http://localhost/example时,$uri就是/example
        # 2.在/data/dist/下尝找这个名example的文件($uri表示这个文件) 或 找这个example的目录($uri表示这个目录)
        # 3.前面两个都找不到,内部请求 /index.html
    }
    # 精准匹配
    location = /api/upload_id {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8001;
    }
    location /media {
        alias /root/sc_api/media_common;
    }
    location /static {
        alias /root/sc_api/static_common;
        autoindex on;
    }
    location / {
        auth_basic "login";
        auth_basic_user_file /data/passwd.txt

        include uwsgi_params; #将所有参数都转到uwsgi下
        uwsgi_pass 127.0.0.1:8001;
        #uwsgi_pass unix:///data/sc_api/my_sock.sock;
        #proxy_pass http://127.0.0.1:8001;
        """
        fastcgi_pass:将请求传递给 fastCGI 服务器。
        uwsgi_pass:将请求传递给 uwsgi 服务器。
        scgi_pass:将请求传递给 SCGI 服务器。
        memcached_pass:将请求传递给 memcached 服务器。
        """
    }
    error_page 404 /404.html;
    location /404.html {
    }
    error_page 500 502 503 /50x.html;
    location =/50x.html {
    }

}

二:ssl cerbot生成证书

server {
    if ($host ==www.example.com){
        return 301 https://$host$request_uri;
    }
    listen 80;
    server_name example.com www.example.com

}
server {
    server_name example.com www.example.com
    client_max_body_size 1024m;

​    location / {
​        proxy_set_header HOST $host;
​        proxy_set_header X-Forwarded-Proto $scheme;
​        proxy_set_header X-Real-IP $remote_addr;
​        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
​        proxy_pass http://127.0.0.1:1000/;
​    }
​    listen 443 ssl;
​    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
​    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
​    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
​    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

三:全局配置

user root root; # 配置用和用户组
woker_precesses auto; # 允许生成的进程数
pid /run/nginx.pid;
includ /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    # 设置日志格式
    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 /var/log/nginx/access.log main;
    # sendfile开启高效文件传输模式,同时搭配二选一:tcp_nopush或tcp_nodelay开启
    # 可防止网络及磁盘I/O阻塞,提升Nginx工作效率
    sendfile            on; # 减少user mode切换到kernel mode,直接在kernel model上做复制
    tcp_nopush          on; # 缓冲区数据装满在发送
    tcp_nodelay         on; # 使缓冲区数据立即发送
    keepalive_timeout   65;
    types_hash_max_size 4096;
}

四:location详解

1:三种类型location
	精准匹配:location = / {}	
	一般匹配:location / {}
	正则匹配:location ~/ {}
2:常用匹配规则
	=:精准匹配
	~:正则匹配,区分大小写
	~*:正则匹配,忽略大小写
	^~:开头匹配,匹配成功后,不在匹配其他的。 location ^~ /index/   http://abc.com/index/index.html
3:location优先级
	location = 精准匹配
	location 完整路径 https://zhuanlan.zhihu.com/p/611501017
	location ^~
	location ~或~*正则匹配
最后交给 / 通配符

五:认证配置

1:生成密码文件
apt install apache2-utilf 或yum install http-tools 或 yum install httpd
htpasswd -cb auth_htpasswd angel lovebeauty3
2:nginx配置说明
auth_basic "提示信息";
auth_basic_user_file auth_htpasswd;

六:禁止php,shell,python目录执行权限

location ~^/images/.*\.(php|php5|sh|pl|py)$
{
    deny all;
}
location ~^/static/.*\.(phh|php5|sh|pl|py)$
{
    deny all;
}
location ~^/data/(attachment|avatar).*\.(php|php5)$
{
    deny all;
}
location ~^/(cadmin|ueditor){
}

七:安全问题

参考

nginx 安装升级安全配置

fastdf

FastDFS防盗链采用token

1.严禁使用root
user nobody nobody
2.配置文件禁止放在web目录中
3.静态文件目录严禁执行脚本
	location ~* ^/(media|static)/.*.(php|php5|python)$ {
    deny all;
	}
4.隐藏版本号
	server_tokens off
5.禁用不需要的http方法
	limit_except GET HEAD POST {deny all;}
6.配置更安全的SSL
7.防止图片盗链
  nginx+fastdfs
 
  
8.防ddos(设置缓冲区大小|设置超时时间)
	client_body_buffer_size  1K;
	client_header_buffer_size 1k;
	client_max_body_size 1k;3
	large_client_header_buffers 2 1k;
	client_body_timeout   10;
	client_header_timeout 10;
	keepalive_timeout     5 5;
	send_timeout          10;
9.限流
	ngx_http_limit_req_module来限制访问频率
	limit_req_zone $binary_remote_addr zone=ip_limit:10m rate=1r/m; # 限制请求频率
	limit_conn_zone $binary_remote_addr zone=ip_conn:10m;  # 限制并连接数

八:nginx进程调度模式

1.无调度模式(各个worker进程争相处理客户端连接,容易惊群)
2.互斥锁模式(各个worker进程争抢互斥锁)
3.socket sharding(套接字分片):由内核提供的一套分配机制,允许每个worker都有一组相同的监听套字节,外部连接时,内核决定那个工作进程接收

九:nginx中负载均衡策略

.轮询(默认)
# 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver {
    server 192.168.0.11;
    server 192.168.0.12;
}

2 weight 权重
# 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver {
    server 192.168.0.11 weight=3;
    server 192.168.0.12 weight=7;
}

3 ip_hash(可用于会话保持)
# 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器 (不能绝对的负载均衡)
upstream backserver {
    ip_hash;
    server 192.168.0.11:8001
    server 192.168.0.12:8002
}
4 fair(第三方)
# 按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver {
    server server1;
    server server2;
    fair;
}

5 url_hash(第三方需装插件)
# 按访问url的hash结果来分配请求,使每个url定向到同一个(对应的)后端服务器,后端服务器为缓存时比较有效。
upstream backserver {
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
}

nginx配置说明
https://centyuan.github.io/2023/11/07/中间件/nginx配置详解/
作者
hlyuan
发布于
2023年11月7日
许可协议