分类 Nginx 下的文章

一、Nginx配置示例

vim /etc/nginx/nginx.conf
……………………

# 使用的用户和用户组
user nginx nginx;

# 指定工作衍生进程数(一般等于CPU的总核数)
worker_processes  2;

# 错误日志的存放路径, 错误日志记录级别可以为{debug|info|notice|warn|error|crit}
error_log  /var/log/nginx/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 指定pid的存放路径
pid        /usr/local/nginx/log/nginx.pid;


events {
    # 使用的网络I/O模型, Linux系统推荐epoll模型,FreeBSD系统推荐使用kqueue模型
    use epoll;
    # 允许的连接数
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    # 设置客户端能够上传的文件大小
    client_max_body_size 8m;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  60;

    #gzip  on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request"'
                 '$status $body_bytes_sent "$http_referer"'
                 '"$http_user_agent" "$http_x_forwarded_for"';

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #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  /usr/local/nginx/log/access.log  main;
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}

    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

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

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

注意:nginx1.12.0+版本需要 将log_format 放置到 server段外部,否则会报一个类似:nginx: [emerg] "log_format" directive is not allowed here in xxx 的错误。

- 阅读剩余部分 -

如果将Web服务器集群当作一个城池,那么负载均衡服务器则相当于城门,重要性不言而喻。如果“城门”关了,与外界的通道也就断了,如果只要一台均衡服务器,当该服务器发生故障时,则会导致整个网站无法访问。因此我们需要来两台以上的服务器,实现故障转移与高可用。

- 阅读剩余部分 -

一、Nginx 安装配置

1.下载并安装Nginx

[root@localhost ~]# yum -y install wget gcc gcc-c++ autoconf automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -r -s /sbin/nologin -g nginx nginx -d /usr/local/nginx

[root@localhost ~]# wget https://nginx.org/download/nginx-1.15.3.tar.gz
[root@localhost ~]# tar zxvf nginx-1.15.3.tar.gz
[root@localhost ~]# cd nginx-1.15.3

[root@localhost nginx-1.15.3]# ./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

[root@localhost nginx-1.15.3]# make 
[root@localhost nginx-1.15.3]# make install

- 阅读剩余部分 -