Nginx - Linux环境搭建 & 与FastDFS整合
一.安装Nginx
安装Nginx目的是, 可以支持通过http://的方式访问Storage中的图片, 也就是让工程中可以在上传图片的同时显示图片
FastDFS的Tracker和Storage需要整合Nginx
所以需要分别在leapfrog-main和leapfrog-sub两台虚拟机安装Nginx
1.安装所有依赖包
yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel
2.安装Nginx
nginx-1.8.0.tar.gz
将nginx-1.8.0.tar.gz拷贝至linux服务器
将安装包拷贝到/user/local/下并解压
cp nginx-1.8.0.tar.gz /usr/local/
cd /usr/local/
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
mkdir -p /var/temp/nginx
./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
make
make install
6.启动Nginx
cd /usr/local/nginx/sbin/
./nginx
若启动时出现端口被占用的情况可以先尝试退出Nginx, 若不行就杀死进程
退出Nginx命令(不需执行):
./nginx -s quit
杀死进程命令(不需执行):
killall -9 nginx
二.在tracker整合Nginx和FastDFS
之前验证Nginx搭建是否成功时已经启动了Nginx, 所以先关闭
./nginx -s quit
1.创建nginx-fdfs.conf配置文件
cd /usr/local/nginx/conf/
vi nginx-fdfs.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#不写此句, 重启虚拟机后再启动Nginx时会报错:nginx: [emerg] open() “/var/run/nginx/nginx.pid” failed (2: No such file or directory))
pid /usr/local/nginx/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;
upstream storage_server_group1{
# 填写Storage所在的电脑ip, 即leapfrog-sub
server 172.16.52.213:80 weight=10;
#server 192.168.101.6:80 weight=10;
}
server {
# 填写之前设置的端口号
listen 80;
server_name ccc.test.com;
# 填写之前设置的组名
location /group1{
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;
proxy_pass http://storage_server_group1;
}
#location /group2{
# 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;
# proxy_pass http://storage_server_group2;
#}
}
# 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;
# }
#}
}
在 /usr/local/nginx 目录下创建 logs 目录:
mkdir /usr/local/nginx/logs
2.启动
cd /usr/local/nginx/sbin/
./nginx -c /usr/local/nginx/conf/nginx-fdfs.conf
若启动时出现端口被占用的情况可以先尝试退出Nginx, 若不行就杀死进程
退出Nginx命令(不需执行):
./nginx -s quit
杀死进程命令(不需执行):
killall -9 nginx
三.在Storage整合Nginx和FastDFS
1.安装fastdfs-nginx-module
fastdfs-nginx-module 作用说明
FastDFS 通过 Tracker 服务器,将文件放在 Storage 服务器存储,但是同组存储服务器之间需要进入 文件复制,有同步延迟的问题。假设 Tracker 服务器将文件上传到了 ip01,上传成功后文件 ID 已经返回给客户端。此时 FastDFS 存储集群机制会将这个文件同步到同组存储 ip02,在文件还 没有复制完成的情况下,客户端如果用这个文件 ID 在 ip02 上取文件,就会出现文件无法访问的 错误。而 fastdfs-nginx-module 可以重定向文件连接到源服务器取文件,避免客户端由于复制延迟导致的 文件无法访问错误。(解压后的 fastdfs-nginx-module 在 nginx 安装时使用)
fastdfs-nginx-module_v1.16.tar.gz
将FastDFS-nginx-module_v1.16.tar.gz传至/usr/local/下
cp fastdfs-nginx-module_v1.16.tar.gz /usr/local/
cd /usr/local
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module/src
修改config文件将三处/usr/local/路径改为/usr/
vi config
将fastdfs-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下
cp mod_fastdfs.conf /etc/fdfs/
修改/etc/fdfs/mod_fastdfs.conf中的内容
vi /etc/fdfs/mod_fastdfs.conf
base_path=/home/FastDFS
tracker_server=172.16.52.214:22122 #Tracker电脑ip地址,即leapfrog-main
url_have_group_name=true #url中包含group名称
store_path0=/home/FastDFS/fdfs_storage #指定文件存储路径
#如果有多个
将libfdfsclient.so拷贝至/usr/lib下
cp /usr/lib64/libfdfsclient.so /usr/lib/
创建nginx/client目录
mkdir -p /var/temp/nginx/client
进入目录(执行安装, 安装成功会在/usr/local/下生成一个nginx文件夹)
cd /usr/local/nginx-1.8.0
./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
–add-module=/usr/local/fastdfs-nginx-module/src
make
make install
2.添加文件配置
新建一个nginx配置文件nginx-fdfs.conf
cd /usr/local/nginx/conf/
vi nginx-fdfs.conf
添加如下内容:
user nobody;
worker_processes 1;
#不写此句, 重启虚拟机后再启动Nginx时会报错:nginx: [emerg] open() “/var/run/nginx/nginx.pid” failed (2: No such file or directory))
pid /usr/local/nginx/logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /group1/M00/{
root /home/FastDFS/fdfs_storage/data;
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
说明:
server_name指定本机ip
location /group1/M00/:group1为nginx 服务FastDFS的分组名称,M00是FastDFS自动生成编号,对应store_path0=/home/FastDFS/fdfs_storage,如果FastDFS定义store_path1,这里就是M01
在 /usr/local/nginx 目录下创建 logs 目录:
mkdir /usr/local/nginx/logs
3.启动
cd /usr/local/nginx/sbin/
./nginx -c /usr/local/nginx/conf/nginx-fdfs.conf
若启动时出现端口被占用的情况可以先尝试退出Nginx, 若不行就杀死进程
退出Nginx命令(不需执行):
./nginx -s quit
杀死进程命令(不需执行):
killall -9 nginx
四.测试
拷贝之前验证FastDFS时Idea打印的图片地址, 若没有了就重新操作一遍即可