Table of Contents
Nginx 介绍
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
PS:反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。同时,用户不需要知道目标服务器的地址,也无须在用户端作任何设定。反向代理服务器通常可用来作为Web加速,即使用反向代理作为Web服务器的前置机来降低网络和服务器的负载,提高访问效率
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx 安装配置
Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
在高连接并发的情况下,Nginx是Apache服务器不错的替代品。
Nginx 安装
系统平台:CentOS release 6.6 (Final) 64位。
一、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、首先要安装 PCRE
PCRE 作用是让 Nginx 支持 Rewrite 功能。
1、下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
[root@toa src]# cd /usr/local/src/
[root@toa src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2、解压安装包:
[root@toa src]# tar zxvf pcre-8.35.tar.gz
3、进入安装包目录
[root@toa src]# cd pcre-8.35
4、编译安装
[root@toa pcre-8.35]# ./configure
[root@toa pcre-8.35]# make && make install
5、查看pcre版本
[root@toa pcre-8.35]# pcre-config --version
8.35
安装 Nginx
1、下载 Nginx,下载地址:http://nginx.org/download/nginx-1.6.2.tar.gz
[root@toa src]# cd /usr/local/src/
[root@toa src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
安装包
[root@toa src]# tar zxvf nginx-1.6.2.tar.gz
3、进入安装包目录
[root@toa src]# cd nginx-1.6.2
4、编译安装
[root@toa nginx-1.6.2]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[root@toa nginx-1.6.2]# make
[root@toa nginx-1.6.2]# make install
5、查看nginx版本
[root@toa nginx-1.6.2]# /usr/local/webserver/nginx/sbin/nginx -v
[root@toa sbin]# ./nginx -v
nginx version: nginx/1.6.2
[root@toa sbin]# pwd
/usr/local/webserver/nginx/sbin
到此,nginx安装完成。
Nginx 配置
创建 Nginx 运行使用的用户 www:
[root@toa conf]# /usr/sbin/groupadd www
[root@toa conf]# /usr/sbin/useradd -g www www
配置nginx.conf ,将/usr/local/webserver/nginx/conf/nginx.conf替换为以下内容
[root@bogon conf]# cat /usr/local/webserver/nginx/conf/nginx.conf
user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#charset gb2312;
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;
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server
{
listen 80;#监听端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
}
启动 Nginx
Nginx 启动命令如下:
[root@toa sbin]# ./nginx -t
nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful
[root@toa sbin]# pwd
/usr/local/webserver/nginx/sbin
访问站点
从浏览器访问我们配置的站点ip:有些问题尚未解决
[root@toa sbin]# ./nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@toa sbin]# sudo netstat -apn | grep 80
tcp6 0 0 :::80 :::* LISTEN 1372/httpd
unix 2 [ ACC ] STREAM LISTENING 25180 1535/master private/bounce
unix 2 [ ACC ] STREAM LISTENING 144683 24379/dockerd /var/run/docker/libnetwork/33650418064b.sock
unix 2 [ ACC ] STREAM LISTENING 25280 1538/postgres /tmp/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 18580 723/lsmd /var/run/lsm/ipc/sim
unix 3 [ ] STREAM CONNECTED 18001 731/dbus-daemon /run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 18090 746/avahi-daemon: r
unix 2 [ ] DGRAM 18091 744/systemd-logind
unix 3 [ ] STREAM CONNECTED 17780 726/abrt-watch-log
unix 3 [ ] STREAM CONNECTED 654780 111032/dbus-daemon
unix 3 [ ] STREAM CONNECTED 22580 1/systemd /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 57691 8068/lvmetad
unix 3 [ ] STREAM CONNECTED 654980 1/systemd /run/systemd/journal/stdout
[root@toa sbin]# ps -ef | grep 1372
root 1372 1 0 14:39 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 51672 1372 0 15:17 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 51673 1372 0 15:17 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 51674 1372 0 15:17 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 51675 1372 0 15:17 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 51676 1372 0 15:17 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 117456 2087 0 16:07 pts/0 00:00:00 grep --color=auto 1372
[root@toa sbin]# systemctl stop apache
Failed to stop apache.service: Unit apache.service not loaded.
[root@toa sbin]# systemctl stop httpd
[root@toa sbin]# systemctl start httpd