| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
8年前发布

Nginx安装

Nginx 是什么

Nginx 是轻量级 开源 稳定 高并发 的HTTP服务器和代理服务器

主要用作我们的图片等静态文件服务器和webserver的代理服务器

安装步骤

下载相关依赖

yum install gcc-c++ 

 yum install pcre pcre-devel 

 yum install zlib zlib-devel 

 yum install openssl openssl—devel

进入到准备安装目录

cd /usr/local

下载nginx:

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

解压nginx到当前目录

tar -zvxf nginx-1.9.9.tar.gz  

添加用户和用户目录:

groupadd www

useradd -g www www -s /sbin/nologin

mkdir -p /data/www

chmod +w /data/www

chown -R www:www /data/www

进入nginx 目录:

cd nginx-1.9.9/

执行:

./configure  --user=www --group=www  /** 默认安装在/usr/local/nginx  */

make 

make install     

 

nginx centos 开机启动

编辑开机启动文件

 vi /usr/lib/systemd/system/nginx.service

[Unit]

Description=nginx - high performance web server

Documentation=https://nginx.org/en/docs/

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

[Install]

WantedBy=multi-user.target

Systemctl 用法

systemctl is-enabled nginx.service #查询服务是否开机启动

systemctl enable nginx.service #开机运行服务

systemctl disable nginx.service #取消开机运行

systemctl start nginx service #启动服务

systemctl stop nginx.service #停止服务

systemctl restart nginx.service #重启服务

systemctl reload nginx.service #重新加载服务配置文件

systemctl status nginx.service #查询服务运行状态

systemctl --failed #显示启动失败的服务

Nginx 详细配置说明

日志文件配置

详细见:

error_log  logs/error.log  info;

access_log  logs/access.log  main;

 

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

 

1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
 2.$remote_user :用来记录客户端用户名称;

 3.$time_local : 用来记录访问时间与时区;

 4.$request : 用来记录请求的url与http协议;

 5.$status : 用来记录请求状态;成功是200,

 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;

 7.$http_referer :用来记录从那个页面链接访问过来的;

 8.$http_user_agent :记录客户端浏览器的相关信息;

 

每个server 可以配置独立的文件 独立分析

配置合适的分隔符方便日志文件分析

日志统一使用##分割方便日后分析

 

 

nginx缓存配置

不配置缓存

nginx 反向代理配置

 

详见测试例子:

upstream dev.zzc {

        server 192.168.1.223:8081;

        ip_hash;

}

upstream test.zzc {

        server 192.168.1.223:18080;

        ip_hash;

}

upstream online.zzc {

        server 192.168.1.223:8085;

        ip_hash;

}

server {

        listen 80;

        server_name test.zzc;

 

        location /SCM/ {

                proxy_pass http://test.zzc;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                index /;

        }

 

 

        location /images/ {

                alias /apps/filebase/test/images/;

        }

        location /resources/ {

                alias /apps/filebase/test/resources/;

        }

 

}

 

server {

        listen 81;

        server_name dev.zzc;

 

        location /SCM/ {

                proxy_pass http://192.168.1.223:8081;

                proxy_redirect  off;

                index /;

        }

        location /images/ {

                alias /apps/filebase/dev/images/;

        }

        location /resources/ {

                alias /apps/filebase/dev/resources/;

        }

 

}

server {

        listen 82;

        server_name online.zzc;

 

        location /SCM/ {

                proxy_pass http://online.zzc;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                index /;

        }

        location /images/ {

                alias /apps/filebase/test_online/images/;

        }

        location /resources/ {

                alias /apps/filebase/test_online/resources/;

        }

 

}

nginx优化

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
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;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";

3、解释一下

第1行:开启Gzip

第2行:不压缩临界值,大于1K的才压缩,一般不用改

第3行:buffer,

第4行:用了反向代理的话 默认是HTTP/1.1

第5行:压缩级别,1-10,数字越大压缩的越好,时间也越长,看心情随便改吧

第6行:进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了

第7行:跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding",我不需要这玩意,自己对照情况看着办吧

第8行:IE6对Gzip不怎么友好,不给它Gzip了

参考文档:

1.http://wenku.baidu.com/link?url=1HnS4A7L9fqQAjShnnGRTw6wkggURA7Hsr8PdvFnqYbaFRqgfS_g6Clk_FPKVbH7S3-phOlkV5ET_dPVASWr3KbFR2Y-jxhr4B3bBIFBQnm

2.http://www.centoscn.com/image-text/install/2014/0812/3480.html

3.http://www.centoscn.com/image-text/install/2014/0812/3480.html

4.http://www.lxway.com/44841214.htm

5.http://my.oschina.net/flys/blog/200497

 

 


来自: http://my.oschina.net/payzheng/blog/546330

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1451802408870.html
Web服务器