#进入下载安装包的路径
cd /mnt/software
# 解压nginx
tar -zxvf nginx-1.22.0.tar.gz
#1、安装gcc库
yum install -y gcc
#2、安装PCRE pcre
yum install -y pcre pcre-devel
#3、安装zlib
yum install -y zlib zlib-devel
#4、安装Open SSL(选择安装)
yum install -y openssl openssl-devel
#第一步,进入刚才解压好的nginx目录
cd /mnt/software/nginx-1.22.1
#第二步,执行命令设置你nginx的安装路径,我这里设置到了/usr/local/nginx下
./configure --prefix=/usr/local/nginx
#第三步,执行make命令生成编译后的文件
make
#第四步,执行make install命令,安装nginx程序
make install
这里要说明一下,在执行第二步的时候,还可以添加更多的参数,例如
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module
,具体添加哪些可以参考下面的说明
其中--
后面的是模块配置,根据你nginx配置文件需要添加相应的模块。如果不是第一次安装nginx,在执行添加模块后的命令之前,先停止nginx服务,然后到/mnt/software/nginx-1.22.1/
执行第二步的命令,然后执行make命令,然后备份/usr/local/nginx/sbin
下的nginx
文件,最后将/mnt/software/nginx-1.22.1/objs
目录下nginx
文件copy到安装路径/usr/local/nginx/sbin
下
以下是更多的安装配置
$./configure --prefix=/usr/local/nginx
--with-openssl=/usr/include (启用ssl)
--with-pcre=/usr/include/pcre/ (启用正规表达式)
--with-http_stub_status_module (安装可以查看nginx状态的程序)
--with-http_memcached_module (启用memcache缓存)
--with-http_rewrite_module (启用支持url重写)
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
cd /usr/local/nginx/sbin 进入/usr/local/nginx/sbin目录
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置
#查看nginx进程
ps -ef|grep nginx
#编辑
vim /etc/rc.local
#最底部增加这一行
/usr/local/nginx/sbin/nginx
nginx -t
即可,该指令会直接指向默认的nginx配置文件nginx -tc /usr/local/nginx/conf/nginx.conf
#yum方式安装的默认地址和配置的默认地址
/etc/nginx/nginx.conf //yum方式安装后默认配置文件的路径
/usr/share/nginx/html //nginx网站默认存放目录
/usr/share/nginx/html/index.html //网站默认主页路径
ps -ef | grep nginx //查看进程apache/httpd
netstat -anpl | grep 'nginx' //查看服务端口
lsof -i //查看端口
traceroute + ip
官方Nginx 配置
Nginx 配置
配置文件样例:
#运行用户
user nobody nobody;
#启动进程
worker_processes 4;
#全局错误日志及PID文档 [ debug | info | notice | warn | error | crit ]
error_log logs/error.log notice;
pid logs/Nginx.pid;
#工作模式及连接数上限
events {
#工作模式有:# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
use epoll;
worker_connections 1024;
}
#设定http服务器,利用他的反向代理功能提供负载均衡支持
http {
#设定mime类型
include conf/mime.types;
default_type application/octet-stream;
#设定日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#开启gzip模块
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#设定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on; #指令激活或停用的sendfile()的用法。
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
#设定虚拟主机
server {
listen 80;
server_name 192.168.8.1 www.yejr.com;
charset utf8;
#设定本虚拟主机的访问日志
access_log logs/www.yejr.com.access.log main;
#假如访问 /img/*, /js/*, /css/* 资源,则直接取本地文档,不通过squid
#假如这些文档较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(img|js|css)/ {
root /data3/Html;
expires 24h;
}
#对 "/" 启用负载均衡
location / {
proxy_pass http://mysvr;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
# error_page 404 /404.html;
# location /404.html {
# root /spool/www;
# charset on;
# source_charset koi8-r;
# }
# location /old_stuff/ {
# rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent;
# }
#location /download/ {
# valid_referers none blocked server_names *.example.com;
# if ($invalid_referer) {
# #rewrite ^/ http://www.example.com/;
# return 403;
# }
# rewrite_log on;
# # rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3
# rewrite ^/(download/.*)/mp3/(.*)\..*$ /$1/mp3/$2.mp3 break;
# root /spool/www;
# # autoindex on;
# access_log /var/log/nginx-download.access_log download;
# }
# location ~* ^.+\.(jpg|jpeg|gif)$ {
# root /spool/www;
# access_log off;
# expires 30d;
#}
}
}
nginx平添stream模块代理sftp:
stream {
#sftp_stream
upstream sftp_srv { #sftp_srv为组名,可自定义命名
hash $remote_addr consistent;
server 192.168.10.183:22 max_fails=3 fail_timeout=60s;
#192.168.10.183:22 为sftp服务器和端口
}
#sftp代理
server {
listen 8000;
#8000端口为sftp服务的代理端口,客户端通过nginx代理登陆sftp服务器将通过此端口。
proxy_connect_timeout 300s;
proxy_timeout 300s;
proxy_pass sftp_srv;
#sftp_srv就是上面配置的upstream sftp_srv
}
}
可以查看 nginx 的错误日志
tail -f /var/log/nginx/error.log