Nginx(“engine x”)是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的Web和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。它的cpu、内存等资源消耗较低,运行非常稳定,因稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。在高连接并发的情况下,Nginx是Apache服务器不错的替代品。
# yum install gcc-c++
# yum install -y pcre pcre-devel
# yum install -y zlib zlib-devel
# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
从Nginx官网http://nginx.org/en/download.html下载安装包并解压到/usr/local下面。

进入安装后的nginx目录中,使用cofigure命令创建一个MakeFile文件。
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \--with-http_stub_status_module \--with-http_ssl_module \--with-file-aio \--with-http_realip_module


在/var下创建temp及nginx目录,-p 表示级联创建的意思。
# mkdir /var/temp/nginx -p
执行make命令进行编译
# make
执行make install 命令进行安装
# make install
查看安装位置/usr/local/nginx目录结构

其中html是里面首页html文件,conf里面是配置文件,sbin里面可执行文件。
进入sbin目录,执行命令./nginx
# ./nginx
查看nginx是否启动
# ps -aux | grep nginx

# ./nginx -s quit(/stop) # 关闭 Nginx
# ./nginx # 启动 Nginx
# ./nginx -s reload # 刷新配置文件
# ./nginx -s reopen # 重启 Nginx
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/rc.d/init.d/iptables save
# service iptables stop
# chkconfig iptables off

在一台服务器启动多个网站,如何区分主要有两种方式:端口不同、域名不同。
配置文件路径:/usr/local/nginx/conf/nginx.conf
原始配置文件内容如下所示
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
可以通过新增配置多个server,从而配置多个虚拟机。
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-new;
index index.html index.htm;
}
首先对nginx进行相应配置,将server_name和root进行修改,然后修改本地hosts配置文件(新增行:当前ip地址 www.baidu.com),复制html目录并进行修改,刷新配置文件后访问域名即可。
server {
listen 80;
server_name www.baidu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-baidu;
index index.html index.htm;
}
# 当前ip地址 www.baidu.com #在hosts配置文件中修改
# cp -r html html-baidu #复制目录
# 可选择修改index.html文件方便区分
# ./nginx -s reload
之前由于项目原因安装了docker镜像,使用起来还是相对简洁易用的,这次准备安装之后好好体验一下Nginx的各大功能,所以又重新安装了正式版本,之后会将遇到的问题和后续的使用体验继续在本文进行更新。
【参考链接】