nginx动静分离
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx动静分离相关的知识,希望对你有一定的参考价值。
一、实验环境:
系统版本:centos6.5 64
nginx版本:nginx-1.2.6
jdk版本:jdk-8u45-linux-x64.rpm
tomcat版本:tomcat-7.0.62.tar.gz
二、nginx安装:
yum install pcre-devel pcre -y
yum -y install openssl openssl-devel
cd /usr/src
wget http://nginx.org/download/nginx-1.2.6.tar.gz
tar -xzf nginx-1.2.6.tar.gz
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make&&make install
三、配置nginx
/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www
mkdir -p /data/www
cd /usr/local/nginx/conf
mv nginx.conf nginx.bak
vi nginx.conf
user www www; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 102400; events { use epoll; worker_connections 102400; } http { include mime.types; default_type application/octet-stream; fastcgi_intercept_errors on; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 4k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; log_format main ‘$http_x_forwarded_for - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" $request_time $remote_addr‘; upstream apachephp { server 172.31.82.178:8080; //这里是代理nginx # server 192.168.1.133 weight=1 max_fails=2 fail_timeout=30s; } include vhosts.conf; }
在同级目录下面创建vhosts.conf
vim vhosts.conf
######www.a.com server { listen 80; server_name 172.31.82.178; index index.html index.htm; location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; 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://apachephp; expires 3d; } location ~ .*\.(php|jsp|cgi)?$ { 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://apachephp; } location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /data/www/; //这里是静态图片目录 expires 3d; } access_log /usr/local/nginx/logs/access.log main; error_log /usr/local/nginx/logs/error.log crit; }
检查Nginx配置文件是否配置正确,提示Ok and successful表示正确
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动Nginx服务
/usr/local/nginx/sbin/nginx
查看Nginx进程是否启动
ps -ef |grep nginx
四、安装tomcat
安装jdk
rpm -ivh jdk-8u45-linux-x64.rpm
配置java环境变量:
export JAVA_HOME=/usr/java/jdk1.8.0_45 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
source /etc/profile
tar zxvf apache-tomcat-7.0.62.tar.gz
mv apache-tomcat-7.0.62 /usr/local/tomcat
启动tomcat:
/usr/local/tomcat/bin/startup.sh
查看tomcat是否启动:
ps -ef |grep tomcat
五、测试动静分离:
在/data/www/ 目录上传一些图片
然后在浏览器使用ip+图片名测试 是否可用调出图片
本文出自 “david0512” 博客,请务必保留此出处http://gjr0512.blog.51cto.com/6518687/1754224
以上是关于nginx动静分离的主要内容,如果未能解决你的问题,请参考以下文章
Nginx系列:Nginx + keepalived 实现高可用 + 防盗链 + 动静分离