Nginx
程序员文章站
2022-06-11 16:13:01
...
一、安装nginx
1、编译环境gcc g++ 开发库之类的需要提前装好
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2、首先安装PCRE pcre功能是让nginx有rewrite功能
下载PCRE:wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
解压安装包:tar zxvf pcre-8.35.tar.gz
进入安装包目录:cd pcre-8.35
编译:./configure
安装:make && make install
查看安装版本:pcre-config --version 如果出现版本号,说明安装成功
3、检查系统里是否安装了pcre软件
rpm -qa pcre 如果没有显示说明没有安装 反之安装过
rpm -e --nodeps pcre 删除pcre
4、安装nginx
下载nginx:wget http://nginx.org/download/nginx-1.6.2.tar.gz
解压安装包: tar zxvf nginx-1.6.2.tar.gz
进入安装包目录:cd nginx-1.6.2
编译安装:./configure 默认地址 /usr/local/nginx
安装:make
安装:make install
5、nginx配置
cd /usr/local/nginx/conf ,把下面的内容覆盖到nginx.conf,内容从菜鸟网站上搜索的
user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/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/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;
}
}
可以检测配置的是否正确
/usr/local/nginx/sbin/nginx -t
5、启动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)…
这说明80接口有被占用,查看接口
netstat -ntpl
kill -9 $pid //杀掉进程
再次启动
/usr/local/nginx/sbin/nginx
停止服务器
/usr/local/nginx/sbin/nginx -s stop 或 /usr/local/nginx/sbin/nginx -s quick
网页访问 127.0.0.1
二、安装ftp远程访问
创建一个系统用户,设置家目录为nginx中设置的static文件夹,并进行其他设置,具体可百度。
三、上传图片至静态资源服务器
1、
public String addGoods(Goods goods,MultipartFile file) {
String imgPath = FileUtil.upLoad(file);
//修改商品表
if (goods != null && !goods.equals("")) {
goods.setGoodsHeadImgPath(imgPath);
goods.setGoodsStatus(0);
goodsService.insertGoods(goods);
}
return "redirect:/goods/preGoods";
}
2、
public class FileUtil {
public static String upLoad(MultipartFile file){
String filename = UUID.randomUUID().toString().replaceAll("-", "");
try {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
//使用FTP进行图片远程上传
try {
FTPClient ftp = new FTPClient();
//FTP服务器的IP和端口
ftp.connect("47.99.221.117", 21);
boolean login = ftp.login("ftpuser2", "123456");
//获取ftp是否响应的状态码
int replyCode = ftp.getReplyCode();
//判断ftp是否正确的回执(一般情况下此处响应失败是因为登录失败了)
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("FTP响应失败");
}
//如果登录成功,开始上传
//配置上传文件的类型(BINARY_FILE_TYPE二进制文件,可以上传所有文件)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//创建一个文件夹,如果文件夹已存在则不会重新创建
ftp.makeDirectory("image");
//将ftp要操作的文件夹指定为images(将图片上传到iamges文件夹下)
ftp.changeWorkingDirectory("image");
InputStream inputStream = file.getInputStream();
//将图片通过ftp写入独立的图片服务器
ftp.storeFile(filename, inputStream);
//退出登录,释放资源
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
}
四、在网页中显示图片
public class FileController {
@RequestMapping("/showImg/{path}")
public void showImg(@PathVariable("path")String imgPath, HttpServletResponse response) {
try {
FTPClient ftp = new FTPClient();
//FTP服务器的IP和端口
ftp.connect("47.99.221.117", 21);
boolean login = ftp.login("ftpuser2", "123456");
//获取ftp是否响应的状态码
int replyCode = ftp.getReplyCode();
//判断ftp是否正确的回执(一般情况下此处响应失败是因为登录失败了)
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("FTP响应失败");
}
//将ftp要操作的文件夹指定为image(将图片上传到iamge文件夹下)
ftp.changeWorkingDirectory("image");
ftp.setBufferSize(2048);
//设置文件类型(二进制)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream inputStream = ftp.retrieveFileStream(imgPath);
OutputStream outputStream = null;
outputStream = response.getOutputStream();
byte[] data = new byte[1024 * 1024];
int len = 0;
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);//输出到指定文件
outputStream.flush();
}
inputStream.close();
outputStream.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
五、定时清理无用图片
public class MyJob {
@Resource
private GoodsService goodsService;
private static final Logger logger = LoggerFactory.getLogger(MyJob.class);
//每隔30秒执行一次
// @Scheduled(cron="*/30 * * * * ?")
//每天凌晨2点执行
@Scheduled(cron=" 0 0 2 * * ?")
public void run() throws Exception {
try{
FTPClient ftp=new FTPClient();
ftp.connect("47.99.221.117",21);
ftp.login("ftpuser2","123456");
int replyCode=ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(replyCode)){
System.out.println("FTP response failed");
}else{
System.out.println("FTP response success");
}
//如果登录成功,开始获取文件列表
//创建一个文件夹,如果文件夹已存在则不会重新创建
ftp.makeDirectory("image");
//将ftp要操作的文件夹指定为images(将图片上传到iamges文件夹下)
ftp.changeWorkingDirectory("image");
String[] listNames = ftp.listNames();
List<String> imgList = goodsService.findAllImgName();
//flag为true时,删除图片,为false时,不删
boolean flag;
for (String fileName : listNames) {
flag = true;
for (String imgPath : imgList) {
//如果该文件名在数据库中存在,表示在使用,不能删除
if(fileName.equals(imgPath)){
flag = false;
continue;
}
}
if(flag){
ftp.dele(fileName);
System.out.println("已删除=="+fileName);
}
}
//退出登录,释放资源
ftp.logout();
}catch(IOException e){
e.printStackTrace();
}
}
}
上一篇: nginx