Nginx快速安装

跟風遠走 提交于 2019-12-21 01:45:07

nginx

nginx安装

安装编译需要的软件

yum install -y gcc gcc-c++

安装Nginx依赖

yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel

创建Nginx用户

useradd -s /bin/false -M nginx

下载Nginx源码

wget http://nginx.org/download/nginx-1.12.0.tar.gz 
tar -zxvf nginx-1.12.0.tar.gz

Nginx 编译安装

# 生成make文件
./configure --user=nginx --group=nginx \
--prefix=/etc/nginx-1.12.0 \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre

# 编译并安装
make && make install
## 添加软连接 
ln -s /etc/nginx-1.12.0/ /etc/nginx
## 添加环境变量 
ln -s /etc/nginx/sbin/* /usr/local/sbin/


Nginx目录介绍

nginx
|-- client_body_temp
|-- conf
|   |-- conf.d
|   |-- default.conf
|   |-- fastcgi.conf
|   |-- fastcgi.conf.default
|   |-- fastcgi_params
|   |-- fastcgi_params.default
|   |-- server-gateway.conf.bak
|   |-- koi-utf
|   |-- koi-win
|   |-- mime.types
|   |-- mime.types.default
|   |-- nginx.conf nginx 子目录存放文件夹
|   |-- nginx.conf.default
|   |-- scgi_params
|   |-- scgi_params.default
|   |-- uwsgi_params
|   |-- uwsgi_params.default
|   `-- win-utf
|-- fastcgi_temp
|-- html
|   |-- 50x.html
|   `-- index.html
|-- logs
|   |-- error.log
|   `-- nginx.pid
|-- proxy_temp
|-- sbin
|   `-- nginx
|-- scgi_temp
`-- uwsgi_temp

Nginx 配置文件详解


location匹配模式以及顺序

  • location = /uri =开头表示精确匹配,只有完全匹配上才能生效
  • location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前
  • location ~ pattern ~开头表示区分大小写的正则匹配
  • location ~* pattern ~*开头表示不区分大小写的正则匹配
  • location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
  • location / 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default

nginx常用命令

  • 检查配置: nginx -t
  • 指定配置文件启动nginx: nginx -c file
  • 启动nginx:nginx
  • 停止nginx: nginx -s stop
  • 平滑停止nginx:nginx -s quit
  • 重载配置:nginx -s reload

配置参考

Web服务器参考

#user  nobody; 
worker_processes  auto; 
#worker_cpu_affinity  auto;
worker_rlimit_nofile 655350;
 
#pid        logs/nginx.pid;
error_log  /data/logs/nginx/error.log error;
 
events {
use epoll;     
worker_connections  655350; 
}

http {    
include       mime.types;    
default_type  application/octet-stream;    
charset utf-8;    
log_format  main '$remote_addr $server_addr $remote_user [$time_local] $host '                    '"$request" $status $body_bytes_sent $request_time $upstream_response_time '                    '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';                    
access_log  /data/logs/nginx/access.log main;
sendfile        on;
keepalive_timeout 90000;    
server_names_hash_max_size 1024;    
server_names_hash_bucket_size 512;    
client_header_buffer_size    16k;    
large_client_header_buffers 4 64k;    
client_header_timeout  300m;    
client_body_timeout    300m;    
send_timeout          300m;    
tcp_nopush     on;    
tcp_nodelay    on;    
client_max_body_size 100M;    
client_body_buffer_size 50m;    
proxy_connect_timeout 5;    
proxy_send_timeout 15;    
proxy_read_timeout 15;    
proxy_buffer_size 256k;    
proxy_buffers 8 256k;    
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;    
proxy_intercept_errors  on;    
proxy_headers_hash_max_size 512;    
proxy_headers_hash_bucket_size 256;    
variables_hash_max_size 512;    
variables_hash_bucket_size 128;    
gzip on;    
gzip_min_length 1100;
gzip_buffers    4 8k;
gzip_comp_level 3;
gzip_http_version 1.0;
gzip_types text/plain application/x-javascript application/json application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
output_buffers 1 32k;    postpone_output 1460;
gzip_vary on;    map_hash_max_size 102400;    map_hash_bucket_size  256;
fastcgi_intercept_errors on;        
server {         
listen 80;         
server_name carlosxiao.cc;         
location / {            
root /data/www;            
index index.html index.htm;        
}        
access_log  /data/logs/nginx/carlosxiao.log;    
}    
}

反向代理

#user  nobody; 
worker_processes  auto; 
#worker_cpu_affinity  auto; 
worker_rlimit_nofile 655350;
#pid        logs/nginx.pid; 
error_log  /data/logs/nginx/error.log error;
 events {
    use epoll;     worker_connections  655350; 
    }
 
 http {    
 include       mime.types;    
 default_type  application/octet-stream;    
 charset utf-8;    log_format  main '$remote_addr $server_addr $remote_user [$time_local] $host '                    '"$request" $status $body_bytes_sent $request_time $upstream_response_time '                    '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';                        access_log  /data/logs/nginx/access.log main;
sendfile        on;    
keepalive_timeout 90000;    
server_names_hash_max_size 1024;    
server_names_hash_bucket_size 512;    
client_header_buffer_size    16k;    
large_client_header_buffers 4 64k;    
client_header_timeout  300m;    
client_body_timeout    300m;    
send_timeout          300m;    
tcp_nopush     on;    
tcp_nodelay    on;    
client_max_body_size 100M;   
client_body_buffer_size 50m;   
proxy_connect_timeout 5;   
proxy_send_timeout 15;    
proxy_read_timeout 15;    
proxy_buffer_size 256k;    
proxy_buffers 8 256k;   
proxy_busy_buffers_size 256k;    
proxy_temp_file_write_size 256k;    
proxy_intercept_errors  on;    
proxy_headers_hash_max_size 512;    
proxy_headers_hash_bucket_size 256;    
variables_hash_max_size 512;    
variables_hash_bucket_size 128;    
gzip on;    
gzip_min_length 1100;
gzip_buffers    4 8k;
gzip_comp_level 3;
gzip_http_version 1.0;
gzip_types text/plain application/x-javascript application/json application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
output_buffers 1 32k;    
postpone_output 1460;
gzip_vary on;    
map_hash_max_size 102400;    
map_hash_bucket_size  256;
fastcgi_intercept_errors on;
 
upstream carlosxiao.cc{        
server 192.168.13.43:8080;        
server 192.168.13.44:8080;        
check interval=3000 rise=2 fall=3 timeout=3000 type=http;    
}        
server {         
listen 80;         
server_name carlosxiao.cc;         
location / {             
proxy_pass http://carlosxiao.cc;             
proxy_set_header Host $host;             
proxy_redirect off;             
proxy_set_header X-Real-IP $remote_addr;             
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;             
proxy_connect_timeout 60;             
proxy_read_timeout 600;             
proxy_send_timeout 600;        
}        
access_log  /data/logs/nginx/carlosxiao.log;    
}    
}

动静分离

#user  nobody; 
worker_processes  auto; 
#worker_cpu_affinity  auto;
worker_rlimit_nofile 655350;
 
#pid        logs/nginx.pid; 
error_log  /data/logs/nginx/error.log error;
 
events {    
use epoll;     
worker_connections  655350; 
}
 
 
http {    include       mime.types;
 default_type  application/octet-stream;    charset utf-8;    log_format  main '$remote_addr $server_addr $remote_user [$time_local] $host '                    '"$request" $status $body_bytes_sent $request_time $upstream_response_time '                    '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';                        access_log  /data/logs/nginx/access.log main;
sendfile        on;   
keepalive_timeout 90000;    
server_names_hash_max_size 1024;    
server_names_hash_bucket_size 512;    
client_header_buffer_size    16k;    
large_client_header_buffers 4 64k;    
client_header_timeout  300m;    
client_body_timeout    300m;    
send_timeout          300m;    
tcp_nopush     on;   
tcp_nodelay    on;    
client_max_body_size 100M;   
client_body_buffer_size 50m;   
proxy_connect_timeout 5;   
proxy_send_timeout 15;    
proxy_read_timeout 15;    
proxy_buffer_size 256k;    
proxy_buffers 8 256k;    
proxy_busy_buffers_size 256k;   
proxy_temp_file_write_size 256k;    
proxy_intercept_errors  on;    
proxy_headers_hash_max_size 512;    
proxy_headers_hash_bucket_size 256;    
variables_hash_max_size 512;    
variables_hash_bucket_size 128;    
gzip on;    
gzip_min_length 1100;
gzip_buffers    4 8k;
gzip_comp_level 3;
gzip_http_version 1.0;
gzip_types text/plain application/x-javascript application/json application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
output_buffers 1 32k;    postpone_output 1460;
gzip_vary on;    map_hash_max_size 102400;    map_hash_bucket_size  256;
fastcgi_intercept_errors on;

upstream carlosxiao.cc{       
 server 127.0.0.1:10179;    
 }        
 server {         
 listen 80;         
 server_name carlosxiao.cc;         
 location /api {            
 proxy_pass http://carlosxiao.cc;             
 proxy_set_header Host $host;             
 proxy_redirect off;            
 proxy_set_header X-Real-IP $remote_addr;             
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;             
 proxy_connect_timeout 60;             
 proxy_read_timeout 600;             
 proxy_send_timeout 600;         
 }        
 
 location ~ .(jsp|jspx|do)?$ {            
 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://carlosxiao.cc;        }                 
 
 #所有静态文件由nginx直接读取不经过tomcat或resin        
 location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|        pdf|xls|mp3|wma)$        {            
 expires 15d;         
 }                 
  location ~ .*.(js|css)?$        {
 expires 1h;        
 }        

 access_log  /data/logs/nginx/carlosxiao.log;    
 }   
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!