2019独角兽企业重金招聘Python工程师标准>>>
以下都以centos为例
1. nginx安装
1) 依赖安装
# yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc
2) 下载nginx本体及缓存module
ngx_cache_purge http://labs.frickle.com/nginx_ngx_cache_purge/
nginx http://nginx.org
3) 创建用户
# useradd nginx
4) 编译nginx及安装
以上下载的2个文件放置在/home/nginx目录下, 并解压
进入nginx目录(不同版本名称不一样), 执行编译前配置, 注意修改最后的ngx_cache_purge插件版本
# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --add-module=../ngx_cache_purge-x.x
# make
# make install
5) 配置nginx
参考 附件nginx.conf
6) 创建缓存目录
以nginx配置文件中设置的目录为准
7) 启动及关闭
在安装时已配置运行用户为nginx, 必须使用root用户启动, 进程将属于nginx用户
启动
/usr/local/nginx/sbin/nginx
关闭
/usr/local/nginx/sbin/nginx -s stop
重新读取配置
/usr/local/nginx/sbin/nginx -s relo
8) 设置开机自启动
在/etc/rc.local目录中增加启动命令即可
也可以使用centos7以后支持的systemctl
9) 防火墙放行nginx侦听端口
centos7以下使用iptables相关命令
centos7以上使用firewall-cmd相关命令
注: 若后端服务器为tomcat注意在/etc/rc.local中先执行source /etc/profile引入JDK配置(前提是JDK的JAVA_HOME等变量设置在profile文件内)
附 nginx.conf
user nginx nginx;
# 工作进程 默认1
worker_processes 2;
error_log /usr/local/nginx/logs/error.log crit;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
#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 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;
# 临时文件目录设置
proxy_temp_path /home/nginx/nginx_cache/proxy_temp_dir;
# 缓存文件目录设置 过期时间设置 这里设置过期时间是1天(1d), 缓存最多30g
proxy_cache_path /home/nginx/nginx_cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
# 后端服务器配置
upstream backend_server {
# 这里设置后端服务器权重, 最大尝试次数, 超时
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8082 weight=1 max_fails=2 fail_timeout=30s;
ip_hash;
}
server {
# nginx侦听端口
listen 80;
# 服务器名称, 不需要和主机名一致
server_name myServerName;
charset utf-8;
# 关闭访问日志, 例如某某IP访问了某资源
access_log off;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_key $host$uri$is_args$args;
# 这里配置了$server_port用于应付nginx端口非80的情况
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-For $remote_addr:$server_port;
proxy_pass http://backend_server;
expires 1d;
}
location ~ /purge(/.*)
{
allow 127.0.0.1;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
#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;
# }
#}
}