nginx手册

nginx手册

Scroll Down

安装更新

编译安装

预备工具

yum -y install pcre-devel
yum install -y zlib-devel
yum install -y gcc gcc-c++
#如果需要SSL的话
yum -y install openssl openssl-devel

下载nginx

wget http://nginx.org/download/nginx-1.16.0.tar.gz

进入文件夹后安装编译

./configure --prefix=/root/nginx --with-http_ssl_module 
#如果不需要SSL的话--with-http_ssl_module可以去掉
make
make install

安装完成后

ln -s 安装目录/nginx /usr/bin #建立软连接

nginx -c conf/nginx.conf #指定配置文件
nginx -s reload #启动

准备工作

语法高亮

进入网上下载的nginx解压后的文件夹

mkdir ~/.vim
cp -r contrib/vim/* ~/.vim

vim中tab制表符改为4个空格

vim ~/.vimrc

然后输入:

set ts=4
set noexpandtab

保存并退出

更新

先重新编译
注意不要make install

先备份原本的nginx

cp -r objs/nginx /root/nginx/sbin/nginx -f

运行新版nginx,并关闭原来的版本

cd /root/nginx/sbin

#查看端口号
grep -ef | grep nginx

#开启新程序
kill -USR2 master进程的端口号
#关闭旧版本
kill -WINCH master进程的端口号

配置文件

反向代理

想将本服务器代理到http://ip:端口

http块中加入

upstream  起个名字{
	server	ip:端口;
}

server	{
        listen 80;
        server_name 域名;

	location / {

		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://起的名字;
	}

}

静态资源

server{
	listen 80;
	server_name 域名;
	
	root	静态资源路径;

	location / {
		# 如果找不到对应资源则访问index.html
		# vue的hostory类型的路由等情况需要用到,否则404
		try_files $uri $uri/ /index.html;

		# 或者使用如下,则为默认进入index.html
		# index	index.html
	}
}

重定向

server{
	listen 80;
        server_name 域名;
        
        location / {
            rewrite ^/(.*)$ http://需要重定向到的域名/$1 permanent;
        }
}

SSL

server {
        listen       443 ssl;
        server_name  域名;        

        ssl_certificate      PEM文件路径.pem;
        ssl_certificate_key  KEY文件路径.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}

压缩

http块中加入

gzip  on;
gzip_min_length 1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image.jpeg image/gif image/png;