005.nginx配置文件

1.替换nginx主配置文件

通过前面的配置,LNMP的环境已经搭建完成,现在我们替换nginx配置文件:

[root@huh ~]# cd /usr/local/nginx/conf/
[root@huh conf]# > nginx.conf
[root@huh conf]# vim nginx.conf

写入后的nginx.conf:

#定义Nginx运行的用户和用户组,系统中必须有此用户,可以是nologin
user nobody nobody;

#启动进程,通常设置成和cpu的数量相等,(设置为“auto”将尝试自动检测它)
worker_processes 2;

#全局错误日志 [debug | info | notice | warn |error | crit]
error_log /usr/local/nginx/logs/nginx_error.log crit;

#进程PID文件
pid /usr/local/nginx/logs/nginx.pid;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 1024;
#
#
#工作模式与连接数上限
events
{
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能(如果跑在FreeBSD上,就用kqueue模型)
    use epoll;
    #单个后台worker process进程的最大并发链接数 (最大连接数=连接数*进程数)
    worker_connections 1024; 
}
#
#
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
    #设定mime类型,类型由mime.type文件定义
    include mime.types;
    #默认文件类型
    default_type application/octet-stream;
    #默认编码
    #charset utf-8; 
    #服务器名字的hash表大小
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    #log_format用来设置日志格式
    log_format huh '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile on;
    #防止网络阻塞
    tcp_nopush on;
    #连接超时时间(单位是秒)
    keepalive_timeout 30;
    #请求头的超时时间
    client_header_timeout 3m;
    #请求体的超时时间
    client_body_timeout 3m;
    #客户端的响应超时时间,这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。
    send_timeout 3m;
    connection_pool_size 256;
    
    #上传文件大小限制
    client_header_buffer_size 1k;
    #设定请求缓存
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    #指令指定"连接请求实体"试图写入的临时文件路径
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    
    #开启gzip压缩
    gzip on;
    #最小压缩文件大小
    gzip_min_length 1k;
    #压缩缓冲区
    gzip_buffers 4 8k;
    #压缩等级
    gzip_comp_level 5;
    #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_http_version 1.1;
    #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
     
    #包含其它配置文件,如自定义的虚拟主机
    include vhosts/*.conf;
}

2.添加nginx虚拟主机配置文件

我们创建vhosts文件夹:

[root@huh conf]# mkdir vhosts
[root@huh conf]# cd vhosts

写入默认虚拟主机配置文件

[root@huh vhosts]# vim default.conf

写入内容:

#默认的虚拟主机配置文件
server
{
    #同样监听80端口
    listen 80 default;
    #定义使用localhost访问
    server_name localhost;
    #默认访问的界面
    index index.html index.htm index.php;
    #该域名可以访问的根目录,(我们可以给该目录加访问权限,让所有人都无法访问!) 
    root /tmp/1233;
    #禁止所有IP访问!(也就是说谁也不能访问默认虚拟主机配置文件访问此网站)
    deny all;
}

 3.写入虚拟主机配置文件test.conf:

[root@huh vhosts]# vim test.conf

写入内容:

#虚拟主机配置文件

server
{
    #监听端口
    listen 80;
    #定义使用www.xxx.com访问
    server_name www.test.com www.aaa.com www.bbb.com;
    #设定本虚拟主机的访问日志,(huh为日志格式,在nginx主配置文件中定义)
    access_log /tmp/access.log huh;
    
    #IP访问控制,禁止某些IP访问网站
    #deny 10.0.0.83;
   
    if ($http_user_agent ~ 'baidu|111111')     
    {         
        return 403;
    }

    #nginx域名跳转,(permanent代表永久重定向,返回http状态301)
    if ($host != 'www.test.com')
    {
        rewrite ^/(.*)$ http://www.test.com/$1 permanent;
    }

    index index.html index.htm index.php;
    #该域名可以访问的根目录
    root /data/www;
    #对指定的目录添加用户认证模块    
    location ~ .*admin.php$ {
        #对该目录允许访问的IP
        allow 10.0.0.83;
        allow 127.0.0.1;
        #对该目录禁止其它IP访问
        deny all;
        #认证提示
        auth_basic "huh's auth";
        #认证所需的"保存用户密码的文件"
        auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
        
        #以下部分为解析php所需
        #将fastcgi_params配置文件包含进来
        include fastcgi_params;
        #fastcgi_pass设置使用何种方式连接php,(php进程使用何种方式监听,这儿也要设置使用何种方式连接,)
        fastcgi_pass unix:/tmp/www.sock;
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        #脚本文件请求的路径,其中$fastcgi_script_name是请求脚本的名称。
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }

    #设置不记录指定文件类型日志
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|mp3|mp4|avi|flv|rar|zip|gz|bz2)$
    {
        #不记录符合此匹配规则的文件
        access_log off;
        #设置静态文件过期时间
        expires 15d;
        #配置防盗链,其中invalid_referer是对valid_referers的否定形式
        valid_referers none blocked *.test.com *.aaa.com *.aminglinux.com;         
        if ($invalid_referer)         
        {             
            return 403;          
        }   
    }
    
    #设置不记录指定文件类型日志
    location ~ (static|cache)
    {
        access_log off;
        expires 2h;
    }
    
    #设置解析php
    location ~ .php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/www.sock;
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
}

注:

1./usr/local/nginx/conf/.htpasswd这个加密文件需要使用apache里面的htpasswd工具去生成,所以使用这个工具需要先安装apache!

假如我们的apache安装在/usr/local/apache2/目录下,则:

[root@huh vhosts]# cd ..
[root@huh conf]# /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd huh
New password: 
Re-type new password: 
Adding password for user huh

注:使用-c参数创建该文件,若文件已存在,则不加-c

4.查看配置文件是否正确

[root@huh vhosts]# /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
[root@huh vhosts]# /usr/local/nginx/sbin/nginx -s reload

访问discuz:

我们去主机的hosts文件(不是虚拟机)中添加一条dns:

[root@huh ~]# vim /etc/hosts
最末行添加的语句(假如当前虚拟机的IP是10.0.0.126):
10.0.0.126   www.test.com www.aaa.com www.bbb.com

在浏览器中输入域名访问:http://www.test.com

访问成功!说明我们配置文件都是正确的!

原文地址:https://www.cnblogs.com/ruo-yu/p/5171092.html