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

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

005.nginx配置文件的更多相关文章

  1. Nginx配置文件nginx.conf中文详解(转)

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...

  2. 查看nginx配置文件路径

    进入nginx安装目录(我的是/usr/local/nginx-1.7.8/) 进入sbin目录,输入 ./nginx -t查看nginx配置文件路径以及该文件语法是否正确 ./nginx -v查看n ...

  3. Nginx配置文件详解

    Nginx是一款面向性能设计的HTTP服务器,相较于Apache.lighttpd具有占有内存少,稳定性高等优势. ######Nginx配置文件nginx.conf中文详解##### #定义Ngin ...

  4. nginx配置文件nginx.conf超详细讲解

    #nginx进程,一般设置为和cpu核数一样worker_processes 4;                        #错误日志存放目录 error_log  /data1/logs/er ...

  5. 通过nginx配置文件抵御攻击

    通过nginx配置文件抵御攻击 囧思九千 · 2013/11/12 12:22 0x00 前言 大家好,我们是OpenCDN团队的Twwy.这次我们来讲讲如何通过简单的配置文件来实现nginx防御攻击 ...

  6. Nginx配置文件nginx.conf详细说明

    Nginx配置文件nginx.conf详细说明 #worker_processes 8; #worker_cpu_affinity 00000001 00000010 00000100 0000100 ...

  7. 【转】Nginx配置文件详细说明

    Nginx配置文件详细说明 在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data;    #启动进程,通常设置成和cpu的数量相等 ...

  8. 一、Nginx配置文件详解

    配置文件介绍 主要有两部分:分别是 main:主体部分 http{}:虚拟主机配置部分 配置指令主要以分号结尾:配置语法:directive value1 [value2 ....] 支持使用的变量 ...

  9. 【Nginx笔记】nginx配置文件具体解释

    本文主要对nginx的配置做重点说明,关于nginx的其他基本概念.建议參考官网描写叙述.这里推荐Nginx Beginner's Guide这篇文档.对刚開始学习的人高速认识nginx非常有帮助. ...

随机推荐

  1. 让你的web程序“动”起来。

    看到这里你可能会问,asp.net程序本身就是动态网站,还要如何动? 我这里所谓的动起来,是指动态加载,动态更新.好吧可能你又要问了动态网站本来就是动态加载,动态更新的.asp.net的程序依附于II ...

  2. 深入剖析tomcat之一个简单的web服务器

    这个简单的web服务器包含三个类 HttpServer Request Response 在应用程序的入口点,也就是静态main函数中,创建一个HttpServer实例,然后调用其await()方法. ...

  3. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  4. webpack+vue-cli项目打包技巧

    1.设置config文件夹index.js中productionSourceMap的值为false,也就是设置webpack配置中devtool为false,打包后文件体积可以减少百分之八十!!!!! ...

  5. 在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效。

    当访问 https 内容的时候,有时候经常会看到证书错误(不在操作系统的证书信任链中?)的提示,在浏览器中我们可以忽略错误的证书,继续访问网页内容. 但是在 .NET 程序中,需要由代码来判断是否忽略 ...

  6. ios h5 app avalon tap点击事件失效及点击延迟300ms问题解决方法

    1.ios h5 app avalon tap事件失效 使用MUI制作app界面,使用avalon.js渲染数据,发现在(Android上正常)ios上运行时容器div的avalon的ms-on-ta ...

  7. Ajax制作智能提示搜索

    一.效果图: 二.实现过程: 思路: 三.部分代码: html: <div id="searchbox"> <div><input type=&quo ...

  8. 向 div 元素添加圆角边框:

    div { border:2px solid; border-radius:25px; }

  9. IDCM项目学习笔记

    项目介绍: IDCM:Internet Data center monitoring 网络数据中心监控平台 IRP:Information Resource planing 信息资源规划 1.设置表中 ...

  10. 【精粹系列】PHP精粹

    本文地址 分享提纲: 1. 概述 2. 精粹内容 2.1 语言结构 2.2 大小写问题 2.3 变量函数 2.4 常量相关函数   2.5 字符串的使用 2.6 函数 2.7 数据库操作 2.8 自动 ...