ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

nginx合集

2021-11-01 21:33:10  阅读:176  来源: 互联网

标签:autoindex etc root server nginx conf 合集


一、nginx安装的三种方式。

    1.yum安装

[root@recall-1 ~]#yum -y install nginx                 #yum安装nginx [root@recall-1 ~]# rpm -qc nginx                       #查看配置文件位置 /etc/logrotate.d/nginx /etc/nginx/fastcgi.conf /etc/nginx/fastcgi.conf.default /etc/nginx/fastcgi_params /etc/nginx/fastcgi_params.default /etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/mime.types /etc/nginx/mime.types.default /etc/nginx/nginx.conf                                         /etc/nginx/nginx.conf.default /etc/nginx/scgi_params /etc/nginx/scgi_params.default /etc/nginx/uwsgi_params /etc/nginx/uwsgi_params.default /etc/nginx/win-utf     

    2.rpm安装

[root@recall-1 ~]# rpm -ihv nginx-1.16.1-1.el7.ngx.x86_64.rpm [root@recall-1 ~]# rpm -qc nginx                       #查看配置文件位置 /etc/logrotate.d/nginx /etc/nginx/fastcgi.conf /etc/nginx/fastcgi.conf.default /etc/nginx/fastcgi_params /etc/nginx/fastcgi_params.default /etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/mime.types /etc/nginx/mime.types.default /etc/nginx/nginx.conf                                         /etc/nginx/nginx.conf.default /etc/nginx/scgi_params /etc/nginx/scgi_params.default /etc/nginx/uwsgi_params /etc/nginx/uwsgi_params.default /etc/nginx/win-utf  

    3.源码安装(推荐)(./configure --help可以查看所有模块)

[root@recall-1 ~]# ls anaconda-ks.cfg  default  nginx-1.20.1.tar.gz [root@recall-1 ~]# tar -xf nginx-1.20.1.tar.gz [root@recall-1 ~]# cd nginx-1.20.1 [root@recall-1 ~]# yum -y install pcre-devel openssl-devel [root@recall-1 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx     #指定位置安装 [root@recall-1 nginx-1.20.1]# make && make install  

4.为以及安装的nginx添加模块(编译安装模式,源码包用同一个版本的)

[root@recall-1 ~]# nginx -V nginx version: nginx/1.20.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) configure arguments: --prefix=/usr/local/nginx [root@recall-1 ~]# ls anaconda-ks.cfg  default  nginx-1.20.1.tar.gz [root@recall-1 ~]# tar -xf nginx-1.20.1.tar.gz [root@recall-1 ~]# cd nginx-1.20.1 [root@recall-1 nginx-1.20.1]# ls auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src [root@recall-1 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module [root@recall-1 nginx-1.20.1]# make                                                              #别make install  要不然直接GG [root@recall-1 nginx-1.20.1]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak    #备份老的二进制文件 [root@recall-1 nginx-1.20.1]# cp objs/nginx /usr/local/nginx/sbin/nginx                         #拷贝新的二进制文件 [root@recall-1 nginx-1.20.1]# nginx -V nginx version: nginx/1.20.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips  26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module        

二、nginx的配置文件详解

1.优化后的nginx配置文件

    user nginx;                 #优化1.工作进程数量 auto为自动        worker_processes auto;                  #pid /usr/local/nginx/logs/nginx.pid;     #优化2.nginx最大文件打开数 worker_rlimit_nofile    65536;          events {             #优化3.nginx事件处理epoll模型              use epoll;                      #优化4.进程允许客户端最大链接数            worker_connections 65535;     }     http {     # Basic Settings   charset UTF-8;     #优化5.开启高速传输配置   sendfile on;   #开启高速传输                  tcp_nopush on;  #数据不会马上发出去,而是等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。 tcp_nodelay on;   #只要有数据包产生,不管大小多少,就尽快传输     types_hash_max_size 2048; #             #优化6.链接超时时间############ # 长连接超时配置 keepalive_timeout  65; client_header_timeout 60s; client_body_timeout 60s; send_timeout 300s;     #优化7.隐藏版本号##############   server_tokens off;                 #优化8.限制文件上传大小      client_max_body_size 8m;           #include /etc/nginx/mime.types; default_type application/octet-stream;     # SSL Settings                   #优化9.开启ssl证书协议        ssl_certificate /usr/local/nginx/conf/server.pem; ssl_certificate_key /usr/local/nginx/conf/server.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on;     # Logging Settings#优化10.前端有防火墙才会用这个日志格式         log_format  main   - []       ;                      access_log /usr/local/nginx/logs/access.log; error_log /usr/local/nginx/logs/error.log;                 #ip limit        #优化11.nginx访问限速         #限制用户连接数来预防DOS攻击         limit_conn_zone $binary_remote_addr zone=perip:10m;         limit_conn_zone $server_name zone=perserver:10m;         #限制同一客户端ip最大并发连接数         limit_conn perip 200;         #限制同一server最大并发连接数 limit_conn perserver 200; #限制下载速度,根据自身服务器带宽配置 limit_rate 3000k;                 #响应头         add_header X-Cache $upstream_cache_status; #        add_header X-Frame-Options SAMEORIGIN;         add_header X-Content-Type-Options nosniff;      # Gzip Settings      #优化12.配置网页压缩gzip     gzip on; gzip_disable "msie6";         gzip_vary on;         gzip_proxied any;         gzip_comp_level 6;         gzip_buffers 16 8k;         gzip_http_version 1.1;         gzip_types image/png text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif;     # Virtual Host Configs   #优化13.设置vhost配置路径      include /usr/local/nginx/conf/vhost/*.conf;      #  /usr/local/nginx/conf/vhost文件夹需要手动创建 }  

2.server虚拟主机介绍

server {     listen       80;                       #虚拟主机使用的端口     server_name  localhost;                #虚拟主机使用的域名     location / {                                                         root   html;                       #虚拟主机web文件家目录         index  index.html index.htm;       #index文件 } }   以上为一个简单的server虚拟主机   1.基于ip的虚拟主机 server {         listen 10.0.0.9:80;         location / {                 root /code/addr1;                 index index.html;         } } 2.基于端口的虚拟主机 server {     listen 80;     server_name localhost;     location / {     root /code/tuixiangzi;     index index.html;     } } 3.基于域名的虚拟主机 server {     listen 80;     server_name www.tuixiangzi.com;     location / {         root /code/tuixiangzi;         index index.html;     } }    

三、nginx的模块安装以及使用

1.目录索引模块

模块:ngx_http_autoindex_module     #添加模块步骤上面有些这里就不在写了   使用语法:     autoindex on;                #开启目录索引     autoindex_exact_size off;    #文件大小格式化(四舍五入)     autoindex_localtime on;      #显示本机时间   添加的位置: http, server, location均可  

2.nginx访问控制模块

模块:ngx_http_access_module   使用语法:         允许访问:                 allow ip | all;         拒绝访问:                 deny address | all ;   添加的位置:http, server, location, limit_except #如果配置允许,则也要配置拒绝;配置拒绝可以单独配置     例题: 拒绝指定ip其他全部允许 [root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server {     listen 80;     server_name www.autoindex.com;     charset utf8;     location / {         root /code/autoindex;         index index.html;     }     location /download {         root /code/autoindex;         autoindex on;         autoindex_exact_size off;         autoindex_localtime on;         allow 10.0.0.1;         #如果使用all,一定放在最后面         deny all;     } }     例题:只允许指定ip,其他全部拒绝。 [root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server {     listen 80;     server_name www.autoindex.com;     charset utf8;     location / {         root /code/autoindex;         index index.html;     }     location /download {         root /code/autoindex;         autoindex on;         autoindex_exact_size off;         autoindex_localtime on;         allow 10.0.0.1;         #如果使用all,一定放在最后面         deny all;     } }   例题:只允许10.0.0.1访问,拒绝改网段其他ip [root@web01 ~]# vim /etc/nginx/conf.d/www.autoindex.com.conf server {     listen 80;     server_name www.autoindex.com;     charset utf8;     location / {         root /code/autoindex;         index index.html;     }     location /download {         root /code/autoindex;         autoindex on;         autoindex_exact_size off;         autoindex_localtime on;         allow 10.0.0.1;         #如果使用all,一定放在最后面         deny 10.0.0.0/24;     } }  

3.访问认证模块

  需要用到的模块工具 模块:ngx_http_auth_basic_module 工具: yum -y install htpasswd   创建密码文件: [root@web01 ~]# htpasswd -c /etc/nginx/auth_basic lhd New password: Re-type new password: Adding password for user lhd #添加一个登录用户 [root@web01 ~]# htpasswd /etc/nginx/auth_basic egon New password: Re-type new password: Adding password for user egon   配置访问登录 使用关键字: auth_basic on; auth_basic_user_file /etc/nginx/auth_basic;   添加位置:http, server, location  

 

4.更多模块配置请前往:

http://shouce.jb51.net/nginx/      

四、常用服务。

1.反向代理(反向代理、负载均衡、七层负载均衡都是这货)

例子: upstream  server {         #server为集群名字可以随便写     server 192.168.159.10:80;                #不加端口默认为80     server 192.168.159.20:80; }   server {         ……     location / {         proxy_pass http://server; } } 分配策略: 轮询(默认): 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 weight(轮询几率): 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的 情况。 ip_hash:用户第一次访问的是那一台服务器,接下来访问的都是那一台服务器。 upstream backserver {     ip_hash;     server 192.168.0.14:88;     server 192.168.0.15:80; } fari(第三方): 按后端服务器的响应时间来分配请求,响应时间短的优先分配。 upstream backserver {     server server1;     server server2;     fair; } url_hash(第三方): 按访问url的hash结果来分配请求,使每个url定向到同一个(对应的)后端服务器,后端服务器为缓存时比较有效。 upstream backserver {     server squid1:3128;     server squid2:3128;     hash $request_uri;     hash_method crc32; }   附加: proxy_pass http://backserver/; upstream backserver{     ip_hash;     server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载)     server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大)     server 127.0.0.1:6060;     server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) }  

2.四层负载均衡

需要安装的模块:--with-srearm 安装后配置文件修改: 把配置文件里面整个http删了,换成: stream { } 案例1:要求使用192.168.15.6的1234端口链接192.168.15.5的22端口             [root@lb02 stream.conf]# cat ssh.conf             upstream ssh {                 server 172.16.1.5:22;             }             server {                 listen 1234;                 proxy_pass ssh;             } 案例2:要求使用192.168.15.6的33060端口代理192.168.15.51的3306端口               [root@lb02 stream.conf]# cat mysql.conf             upstream mysql {                 server 172.16.1.51:3306;             }                 server {                 listen 33060;                 proxy_pass mysql;             }  

3.https加密(单机)。

模块:--with-http_ssl_module   关于实验用证书获取的两种方式: 1.阿里云免费申请。(有小绿锁) 2.自己生成。(没有小绿锁) 生成证书: openssl genrsa -out cert.key openssl req -new -x509 -key cert.key -out cert.pem   修改配置文件(添加) server {     listen       443 ssl;     ……             ssl_certificate      /etc/local/nginx/conf/cert.pem;             ssl_certificate_key  /etc/local/nginx/conf/cert.key;         ssl_session_cache    shared:SSL:1m;         ssl_session_timeout  5m;             ssl_ciphers  HIGH:!aNULL:!MD5;         ssl_prefer_server_ciphers  on;     }   测试修改是否正确 [root@web02 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful   重启nginx [root@web02 conf]# /usr/local/nginx/sbin/nginx -s reload  

4.全站https。

以下是配置没什么好说的了   upstream backserver {         server 172.16.1.7:80;         server 172.16.1.8:80;         server 172.16.1.9:80; }     server {     listen 80;     server_name www.mycentos.top;     return 301 https://www.mycentos.top; } server {     listen 443 ssl;     server_name www.mycentos.top;     ssl_certificate      /usr/local/nginx/conf/server.pem;     ssl_certificate_key  /usr/local/nginx/conf/server.key;     ssl_prefer_server_ciphers  on;     location / {         proxy_pass http://backserver; } }      

 ······待完善········

标签:autoindex,etc,root,server,nginx,conf,合集
来源: https://www.cnblogs.com/RRecal/p/15496269.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有