The following all take centos as an example
1. nginx installation
1) Dependency installation
# yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc
2) Download nginx body and cache module
ngx_cache_purge http://labs.frickle.com/nginx_ngx_cache_purge/
nginx http://nginx.org
3) Create user
# useradd nginx
4) Compile and install nginx
The two files downloaded above are placed in the /home/nginx directory, and decompressed
into the nginx directory (different versions have different names), execute the configuration before compilation, and pay attention to modifying the last version of the ngx_cache_purge plugin
# ./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)
Refer to the attachment nginx.conf for configuring nginx
6) Create a cache directory
The directory set in the nginx configuration file shall prevail
7) Start and shut down
The running user has been configured as nginx during installation, it must be started with the root user, and the process will belong to the nginx user
.
/usr/local/nginx/sbin/nginx
closure
/usr/local/nginx/sbin/nginx -s stop
read configuration again
/usr/local/nginx/sbin/nginx -s relo
8) Set up self-startup
at boot. Add the startup command to the /etc/rc.local directory,
or use systemctl supported by centos7 later.
9) The firewall allows nginx to listen to the port
below centos7 and use the iptables related commands
. Above centos7, use the firewall-cmd related commands
Note: If the backend server is tomcat, please first execute source /etc/profile in /etc/rc.local to introduce JDK configuration (provided that JDK's JAVA_HOME and other variables are set in the profile file)
Attach 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;
# }
#}
}