Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好
安装教程:
http://nginx.org/en/linux_packages.html#stable
nginx是由C语言写,所以需要在centos系统中安装相关解析库。
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
步骤
vim /etc/yum.repos.d/nginx.repo
输入:可以理解为Nginx的下载地址
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
安装 yum install nginx 控制台没报错,就没问题了。
常用命令 查看版本: nginx -v 查看安装的路径以及配置文件的路径: rpm -ql nginx 启动:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
重启:
/usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
关闭:
/usr/local/nginx/sbin/nginx -s stop /usr/local/nginx/conf/nginx.conf
安装完成后,还需做好相关配置才能进行使用。 其中主要的配置文件就是nginx.conf
cd /etc/nginx/nginx.conf
这个配置文件不用动,看最后一行
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
从nginx.conf的最后一行配置可以看到,只需去该目录/etc/nginx/conf.d/下新增自己的配置文件就能完成。
在/etc/nginx/conf.d目录下添加你自定义的配置。可以参考以下的
my_server.conf
server {
listen 80;
server_name localhost 120.79.226.4;
sendfile on;
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
### 反向代理,此处是80端口,可以代理8080端口中的数据。
location ~ /my_proxy.html$ {
proxy_pass http://127.0.0.1:8080;
}
### 设置防盗链
location ~ .*\.(jpg|gif|png)$ {
#gzip on; #gzip压缩
#gzip_http_version 1.1;
#gzip_comp_level 2;
#gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
valid_referers none blocked 120.79.226.4;
if ($valid_referer) {
return 403;
}
root /opt/app/code/images;
}
### 压缩图片
location ~ .*\.(jpg|gif|png)$ {
#gzip on; #gzip压缩
#gzip_http_version 1.1;
#gzip_comp_level 2;
#gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/images;
}
### 压缩文本
location ~ .*\.(txt|xml)$ {
#gzip on; #gzip压缩
#gzip_http_version 1.1;
#gzip_comp_level 2;
#gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/doc;
}
### 解压缩
location ~ ^/download {
gzip_static on; #可读取.gz文件,不推荐。
tcp_nopush on;
root /opt/app/code;
}
### 缓存
#location ~ .*\.(htm|html)$ {
# expires 24h;#过期时间,缓存
# root /opt/app/code;
#}
### 跨域访问
location ~ .*\.(htm|html)$ {
#add_header Access-Control-Allow-Origin http://www.xbmchina.cn;
#add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
root /opt/app/code;
}
location / {
root /usr/share/nginx/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 /usr/share/nginx/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;
#}
}
访问静态资源:http://120.79.226.4/gz.jpg 其他的成果需要相应的场景才能体现,暂无法展现。从上可以看得出Nginx其实就可以搭建一个服务器。