本文为大家介绍一下nginx配置的相关介绍,供大家参考。
user nobody nobody;
#启动进程
worker_processes 2;
#全局错误日志及PID文档
error_log logs/error.log notice;
pid logs/Nginx.pid;
#工作模式及连接数上限
events {
use epoll;
worker_connections 1024;
}
#设定http服务器,利用他的反向代理功能提供负载均衡支持
http {
#设定mime类型
include conf/mime.types;
default_type application/octet-stream;
#设定日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#开启gzip模块
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#设定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#设定负载均衡的服务器列表
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 {
listen 80;
server_name 192.168.8.1
www.
;
charset gb2312;
#设定本虚拟主机的访问日志
access_log logs/www..access.log main;
#假如访问 /img/*, /js/*, /css/* 资源,则直接取本地文档,不通过squid
#假如这些文档较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(img|js|css)/ {
root /data3/Html;
expires 24h;
}
#对 "/" 启用负载均衡
location / {
proxy_pass http://mysvr;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
}
}
备注:conf/htpasswd 文档的内容用 apache 提供的 htpasswd 工具来产生即可,内容大致如下:
3.) 查看 Nginx 运行状态 输入地址http://192.168.8.1/NginxStatus/ 。
输入验证帐号密码,即可看到类似如下内容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
第一行表示现在活跃的连接数,第三行的第三个数字表示Nginx运行到现在的连接数。
紧接上文(nginx上支持php)---
5、修改Linux Nginx的配置文件,支持PHP
# vi /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 8;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 1024;
events
{use epoll;
worker_connections 1024;}
http{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name www.abcdefg.com;
root /var/www/blog;
index index.html index.htm index.php;
location ~ .*\.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/blog$fastcgi_script_name;
includefastcgi_params;}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{expires 30d;}
location ~ .*\.(js|css)?$
{expires 1h;}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/logs/access.log access;}}
注:server部分为PHP虚拟主机127.0.0.1:9000为fastcgi的PC,我这里用的本机/var/www/blog$fastcgi_script_name; 为PHP网页保存的目录测试配置文件:
# /usr/local/nginx/sbin/nginx -t
6、优化Linux内核参数
# vi /etc/sysctl.conf
在末尾增加以下内容:
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 500065000
使配置立即生效:# /sbin/sysctl -p。
本文的实验环境为:Centos4.5,nginx版本为:nginx-0.7.26。
pcre-7.8.tar.gz 正则表达式下载地址:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
nginx-0.7.26.tar下载地址:http://www.nginx.net/
php-5.2.6.tar.bz2下载地址:http://www.php.net/releases/
php-5.2.6-fpm-0.5.9.diff.gz
php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi下载地址:http://php-fpm.anight.org/
注:PHP的版本要和fpm的版本一致mysql-5.0.67.tar.gz
Discuz!_6.0.0_SC_UTF8.zip
1、安装pcre
# cd pcre-7.8
# ./configure
# make && make install
2、安装Nginx
# tar -zxvf nginx-0.7.26.tar.gz
# cd nginx-0.7.26
# ./configure --prefix=/usr/local/nginx
# make && make install
启动nginx# /usr/local/nginx/sbin/nginx
停止nginx# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
重启nginxkill -HUP `cat /usr/local/nginx/logs/nginx.pid`
添加到自启动# echo "/usr/local/nginx/sbin/nginx">>/etc/rc.local
3、安装mysql
# tar -zxvf mysql-5.0.67.tar.gz
# cd mysql-5.0.67
# groupadd mysql
# useradd -g mysql -s /sbin/nologin -M mysql
# ./configure --prefix=/usr/local/mysql --with-charset=gbk --with-extra-charset=all --enable-hread-safe-client
--enable-local-infile --with-low-memory
# make && make install
# cp support-files/my-medium.cnf /etc/my.cnf
# chown -R mysql.mysql /usr/local/mysql/
# /usr/local/mysql/bin/mysql_install_db --user=mysql
# chown -R root.root /usr/local/mysql/
# chown -R mysql.mysql /usr/local/mysql/var/
启动数据库服务,并添加到自启动:
# /usr/local/mysql/bin/mysqld_safe --user=mysql &
#cp support-files/mysql.server /etc/rc.d/init.d/mysqld
#chmod 755 /etc/rc.d/init.d/mysqld
加入自动启动服务队列:
#chkconfig --add mysqld
#chkconfig --level 345 mysqld on添加root密码
# /usr/local/mysql/bin/mysqladmin -u root password "123456"
测试一下:# /usr/local/mysql/bin/mysql -u root -p输入密码:123456,看能不能进入到数据库
配置库文件搜索路径:
# echo "/usr/local/mysql/lib/mysql">>/etc/ld.so.conf
# ldconfig
# ldconfig -v
添加/usr/local/mysql/bin到环境变量PATH中
#echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
#source /etc/profile
4、安装PHP
这里产生的是可执行文件,和apache的不一样,和apache结合的时候产生的是动态库
# gzip -cd php-5.2.6-fpm-0.5.9.diff.gz |patch -d php-5.2.6 -p1
# cd php-5.2.6
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --enable-fastcgi --enable-fpm
--with-config-file-path=/usr/local/php/etc --enable-force-cgi-redirect
# make && make install
# cp php.ini-recommended /usr/local/php/etc/php.ini
# vi /usr/local/php/php-fpm.conf
(1)<value name="listen_address">127.0.0.1:9000</value>修改为<value name="listen_address">IP:9000</value>
//本机就用默认的127.0.0.1
(2)下面这两行去掉注释并修改
<value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
<value name="display_errors">1</value>
(3)<value name="user">nobody</value> //去注释
(4)<value name="group">nobody</value> //去注释
(5)<value name="allowed_clients">127.0.0.1</value> //允许连接的PC,本机就用127.0.0.1
启动php-fpm# /usr/local/php/sbin/php-fpm start添加到自启动# echo "/usr/local/php/sbin/php-fpm start">>/etc/rc.local