一、安装
# 更新源
apt-get update
# 安装
apt-get install nginx
安装完毕以后,通过 nginx -V 查看是否成功,改命令将显示其版本信息。
此时访问ip,则会跳转到nginx的主页,也就是 /usr/share/nginx/html/index.html 。
二、配置
1、目录结构
使用 tree /etc/nginx 查看其目录结构。
总目录: 安装完成后,nginx的目录位于/etc/nginx。
nginx/
├── conf.d
├── fastcgi_params -----------fastcgi相关参数的配置文件
├── koi-utf
├── koi-win
├── mime.types -----------媒体类型
├── naxsi_core.rules
├── naxsi.rules
├── naxsi-ui.conf.1.4.1
├── nginx.conf -----------nginx的服务配置
├── proxy_params
├── scgi_params
├── sites-available -----------当前配置的文件的软连接
│ ├── default
├── sites-enabled -----------sites-enabled目录中对应软连接的真实配置文件
│ ├── default -> /etc/nginx/sites-available/default
├── uwsgi_params
└── win-utf
2、nginx.conf配置优化
user www-data;
#########CPU核心数#########
worker_processes 4;
#########为每个nginx进程分配CPU,利用多核心优势#########
#worker_cpu_affinity 00000001 00000010 00000011 00000100;
#########进程打开的最多文件描述符数目#########
worker_rlimit_nofile 655350;
pid /run/nginx.pid;
events {
worker_connections 10240;
# multi_accept on;
use epoll;
}
http {
##
# Basic Settings
##
#########实现文件传输性能提升###################
client_max_body_size 256m;
large_client_header_buffers 4 512k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
#########禁止nginx页面返回版本信息##############
server_tokens off;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
log_format main '$remote_addr:$remote_port|$upstream_addr [$time_local] "$request_method $request_uri $server_protocol $status $body_bytes_sent $request_time" "$http_referer" "$http_user_agent" "$upstream_addr $upstream_status $http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
#######################gzip压缩功能设置###############################
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript application/x-font-woff image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
################启用keepalive_timeout############
keepalive_timeout 65;
##############设置客户端的响应超时时间.如果客户端停止读取数据,在这么多时间之后就释放过期的客户端连接###############
send_timeout 120;
##############在客户端停止响应之后,允许服务器关闭连接,释放socket关联的内存#############
reset_timedout_connection on;
################分页面大小##########
client_header_buffer_size 512k;
################缓存检查频率########
open_file_cache_valid 30s;
################缓存时间(20s)内的最少使用次数-未超过则移除########
open_file_cache_min_uses 10;
################是否开启缓存错误信息############
open_file_cache_errors on;
##########打开自定义重定向错误页面################
proxy_intercept_errors on;
##########动态访问proxy缓存配置################
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=cache_one:10m max_size=1G inactive=1d;
#########获取真实IP访问头######################
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
三、测试
不去动default,在/etc/nginx/sites-available下新建一个test.com文件,并配置。
server {
listen 81;
server_name baidu;
location / {
proxy_connect_timeout 10; #单位为秒
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_buffer_size 128k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 6 256k; #proxy_buffers缓冲区,网页平均在100k以下的话>,这样设置
proxy_busy_buffers_size 512k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_pass http://www.baidu.com;
}
}
在/etc/nginx/sites-enabled下添加软连接
# 添加软连接
ln -s /etc/nginx/sites-available/test.com test.com
# 重载配置
nginx -s reload
访问ip:81,则会跳转到baidu搜索页面。
四、常用命令
# 验证配置是否正确
nginx -t
# 查看Nginx的详细的版本号
nginx -V (详细信息)
nginx -v (简要信息)
# 启动
start nginx
# 快速停止或关闭
nginx -s stop
# 正常停止或关闭
nginx -s quit
# 配置文件修改重装载
nginx -s reload