[Nginx]Nginx的基本配置与优化1(完整配置示例与虚拟主机配置)

时间:2023-03-08 20:05:03

----------------------------------------------------------------------------------------

完整配置示例:

【 nginx.conf 】

#运行用户
user www-data;
#启动进程, 通常设置成和cpu的数量相等; 用cat /proc/cpuinfo查看cpu信息, cpu cores一栏显示内核数
worker_processes 1; #全局错误日志及PID文件(/usr/local/nginx/logs/error.log)
error_log /var/log/nginx/error.log;
#进程id存储文件(/usr/local/nginx/logs/nginx.pid)
pid /var/run/nginx.pid;
#工作模式及连接数上限
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections 1024; #单个后台worker process进程的最大并发链接数
# multi_accept on; #在nginx获得有新连接的通知之后,接受尽可能多的连接, 如果worker_connections设置太低的话,这样可能会造成拥堵
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义,可以打开mime.type来看(/usr/local/nginx/conf/mime.types)
#MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.
include /etc/nginx/mime.types;
#默认文件类型以二进制数据传输
default_type application/octet-stream;
#设定日志文件(/usr/local/nginx/logs/access.log)
access_log /var/log/nginx/access.log; #关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
server_tokens off; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on; #告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送
#tcp_nopush on; #给客户端分配keep-alive连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65; #告诉nginx不要缓存数据,而是一段一段的发送;当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值
tcp_nodelay on; #开启gzip压缩
gzip on;
#指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。
gzip_disable "MSIE [1-6]\.(?!.*SV1)"; #设定请求缓冲
、、 client_header_buffer_size 1k;
、、 large_client_header_buffers 4 4k; open_file_cache max=100000 inactive=20s; #打开缓存的同时也指定了缓存最大数目,以及缓存的时间; 我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。
open_file_cace_valid 30s; #在open_file_cache中指定检测正确信息的间隔时间
open_file_cache_min_uses 2; #定义了open_file_cache中指令参数不活动时间期间里最小的文件数
open_file_cache_errors on; #指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。 #虚拟主机的配置文件
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*; #设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
} server {
#侦听80端口
listen 80;
#定义使用www.xx.com访问
server_name www.xx.com;
#设定本虚拟主机的访问日志
access_log logs/www.xx.com.access.log main; #默认请求
location / {
root /root; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称
fastcgi_pass www.xx.com;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
} # 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root;
} #静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
} #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
include fastcgi_params;
} #设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
} #禁止访问 .htxxx 文件
location ~ /\.ht {
deny all;
} }
}

其他优化文章:http://www.oschina.net/translate/nginx-setup

-------------------------------------------------------------------------------------------------------

" 手动配置基于域名的虚拟主机 "
步骤:
1. 在nginx.conf中的http段中, 一个server段就表示一个服务主机, 每添加一个虚拟主机, 增加一个server段即可;
2. 如果单独新建目录存放虚拟主机配置文件, 只需要在http段中最后include进来, 如: include vhost/dog.farwish.conf;
3. 这个dog.farwish.conf中只有server段的配置内容, 如:

server {
listen ;
server_name dog.farwish.com; location / {
index index.html index.htm index.php;
root /home/www/dog;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/?s=$ last; # TP rewrite模式
break;
} autoindex on;
} location ~ \.php {
root /home/www/dog;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 以上配置是在nginx1..2源码包配置文件的基础上改动.

4. 编辑hosts文件, vim /etc/hosts, 加入127.0.0.1   dog.farwish.com

-------------------------------------------------------------------------------------------------------

一键安装包中的配置:

  1. 绑定域名

    vim /etc/hosts  #添加格式如:192.168.1.11 www.chenwei.ws

  2. 添加虚拟主机   

    cd到根目录, 一键安装包安装的运行vhost.sh添加虚拟主机, 输入域名即可

  3. 编辑配置文件

    vim /usr/local/nginx/conf/vhost/www.chenwei.ws.conf #配置如下:

 

server
  {
    listen ;
    #listen [::]:;
    server_name www.chenwei.ws;           #服务器名称
    index index.html index.htm index.php default.html default.ht m default.php;
    root /home/wwwroot/default/www.chenwei.ws;  #主机访问对应目录     include other.conf;
    #error_page /.html;
  }   #跨域调用字体时添加
  location ~* \.(eot|ttf|woff)$ {
    add_header Access-Control-Allow-Origin *;
  }

  4. PHP配置open_basedir(将用户访问文件的活动范围限制在指定的区域, open_basedir即为访问目录):

    vim /usr/local/php/etc/php.ini  

   使用命令G 将光标移至行尾, 配置php访问的主机根目录; 如果正确, 则不需要更改:

    [HOST=www.chenwei.ws]

    [PATH=/home/wwwroot/default/www.chenwei.ws]

    open_basedir=/home/wwwroot/default/www.chenwei.ws/:/tmp/

Link: http://www.cnblogs.com/farwish/p/4042525.html