nginx教程

来源:互联网 发布:linux 删除svn版本库 编辑:程序博客网 时间:2024/06/05 07:54

一.yum安装nginx

yum install nginx

yum会自动安装nginx 需要的依赖和nginx

二. 手动安装

2.1下载nginx压缩包:
http://nginx.org/
2.2 解压nginx

tar -zxvf nginx-1.13.1.tar.gz

2.3 安装nginx依赖包

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

2.4 制定安装到的目录

./configure --prefix=/usr/local/nginx

2.5 编译安装

make && make install

三. nginx相关命令
启动: nginx
停止:nginx -s stop
重启:nginx -s reload 或 service nginx reload
检查配置文件:nginx -t /etc/nginx/nginx.conf
启动指定配置文件: nginx -c /etc/nginx/nginx.conf

四.配置文件

user  nginx; #用户worker_processes  1; #进程数,根据cpu核心数设置error_log  /var/log/nginx/error.log warn; #错误日志文件pid        /var/run/nginx.pid;  #进程pid 存放目录events {    worker_connections  1024; #最大并发数}http {    include       /etc/nginx/mime.types;  #包含的配置文件    default_type  application/octet-stream;     #日志显示信息设置    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main; #访问日志#设置为on表示启动高效传输文件的模式。sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。如果这个参数不开启,会先在用户空间(Nginx进程空间)申请一个buffer,用read函数把数据从磁盘读到cache,再从cache读取到用户空间的buffer,再用write函数把数据从用户空间的buffer写入到内核的buffer,最后到tcp socket。开启这个参数后可以让数据不用经过用户buffer。    sendfile        on;    #当使用sendfile函数时,tcp_nopush才起作用。一般情况下,在tcp交互的过程中,当应用程序接收到数据包后马上传送出去,不等待,而tcp_cork选项是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞,已经是默认了。也就是说tcp_nopush = on 会设置调用tcp_cork方法,这个也是默认的,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。    #tcp_nopush     on;#保持连接时间    keepalive_timeout  65;    #开启gzip压缩    #gzip  on;     #不压缩临界值,大于1K的才压缩,一般不用改    #gzip_min_length 1k;    #不用改    #gzip_buffers 4 16k;    #用了反向代理的话,末端通信是HTTP/1.0。默认是HTTP/1.1    #gzip_http_version 1.0;    #压缩级别,1-10,数字越大压缩的越好,时间也越长    #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;    #跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding"    #gzip_vary off;    #IE6对Gzip不怎么友好,不给它Gzip了    #gzip_disable "MSIE [1-6]\.";    #还需要加载的配置文件    include /etc/nginx/conf.d/*.conf;}
#负载均衡配置地址,结合proxy_pass 使用upstream my-project{#会把同一用户的请求发送到同一台服务器    ip_hash;   server 127.0.0.1:8080 weight=2; #weight 权重。默认为1。权重越高处理的请求越多  server 127.0.0.1:8081;}#server 可以配置多个server {    listen       80; #监听的端口     server_name  localhost; #服务地址。可以是域名    #charset koi8-r; #编码    #access_log  /var/log/nginx/log/host.access.log  main; #访问日志    #访问地址    location / {        #nginx root 路径        root   /usr/share/nginx/html;        #默认访问页面        index  index.html index.htm;    }    #404页面    #error_page  404              /404.html;   #50X 的错误跳转页面    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   /usr/share/nginx/html;    }#~ 区分大小写,以.php结尾   location ~ \.php$ {       #代理转发的地址       proxy_pass   http://my-project;       #允许访问的ip       allow 127.0.0.1;       #拒绝访问所有       deny  all;   }location /api/v1.1{#设置跨域    if ($request_method = 'OPTIONS') {         add_header Access-Control-Allow-Origin *;         add_header Access-Control-Allow-Credentials true;         add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';         add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';         return 200;        }        add_header Access-Control-Allow-Origin *;        add_header Access-Control-Allow-Credentials true;        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';        proxy_set_header Host $host:$server_port;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Real-URI http://$host/api/v1$uri$is_args$args;        #重写: http://127.0.0.1/api/v1.1/123 重写成 http://127.0.0.1/123        rewrite  ^/api/v1.1/(.*)  /$1 break;        proxy_pass   http://my-project;    }    location ^~ /initpage {        #转到本地路径 如 http://127.0.0.1/initpage/1.jpg 会到本地路径/opt/bckefu/v1.1/initpage/1.jpg 找1.jpg 文件。        alias /opt/bckefu/v1.1/initpage/;    }    location /static {        #nginx root 路径        root   /usr/share/nginx/html;        autoindex on; #显示本地目录文件夹和文件    }}
原创粉丝点击