Nginx使用及配置

一、Nginx服务

systemctl status nginx.service
systemctl start nginx.service
systemctl stop nginx.service

二、Nginx工作方式:

Nginx在启动后,会有一个master进程和多个worker进程。master进程主要用来管理worker进程,包含:接收来自外界的信号,向各worker进程发送信号,监控worker进程的运行状态,当worker进程退出后(异常情况下),会自动重新启动新的worker进程。而基本的网络事件,则是放在worker进程中来处理了。多个worker进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。一个请求,只可能在一个worker进程中处理,一个worker进程,不可能处理其它进程的请求。worker进程的个数是可以设置的,一般我们会设置与机器cpu核数一致,这里面的原因与nginx的进程模型以及事件处理模型是分不开的。

Nginx的进程模型,可以由下图来表示:
进程模型

Nginx的配置系统由一个主配置文件和其他一些辅助的配置文件构成。这些配置文件均是纯文本文件,全部位于nginx安装目录下的conf目录下。配置文件中以#开始的行,或者是前面有若干空格或者TAB,然后再跟#的行,都被认为是注释,也就是只对编辑查看文件的用户有意义,程序在读取这些注释行的时候,其实际的内容是被忽略的。由于除主配置文件nginx.conf以外的文件都是在某些情况下才使用的,而只有主配置文件是在任何情况下都被使用的。所以在这里我们就以主配置文件为例,来解释nginx的配置系统。在nginx.conf中,包含若干配置项。每个配置项由配置指令和指令参数2个部分构成。指令参数也就是配置指令对应的配置值。
sw_test的nginx.conf


#user  nobody;
worker_processes  16;

#error_log  /mnt/sw/nginx_logs/error.log;
#error_log  /mnt/sw/nginx_logs/error.log  notice;
#error_log  /mnt/sw/nginx_logs/error.log  info;

pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  40960;
}


http {
    include       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  /mnt/sw/nginx_logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  60;

    #gzip  on;
    upstream fcgi_backend {
	#### http 1 or 2
        #server 172.19.10.3:9001;
        #server 172.19.106.7:9001;
        server 10.20.10.3:9001;
        #server 10.20.10.5:7080;

        #keepalive 16;
    }

    server {
        listen       7080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

	location /SimulatedTrading/{    
	      fastcgi_buffer_size 128k;
 	      fastcgi_buffers 16 128k;
	      #root   html;
	      fastcgi_connect_timeout 300;
	      fastcgi_send_timeout 300;
              fastcgi_read_timeout 300;
	      fastcgi_pass fcgi_backend;
              #fastcgi_keep_conn on; 
	      #fastcgi_pass unix:/dev/shm/fcgi.sock; 
	      fastcgi_index index;
	      fastcgi_param GATEWAY_INTERFACE CGI/1.1;
	      fastcgi_param SCRIPT_FILENAME /mnt/sw/run/http/$fastcgi_script_name;
	      include fastcgi_params;
	}

        #error_page  404              /404.html;
	location /test/{    
	      fastcgi_buffer_size 128k;
 	      fastcgi_buffers 16 128k;
	      #root   html;
	      fastcgi_connect_timeout 300;
	      fastcgi_send_timeout 300;
              fastcgi_read_timeout 300;
	      fastcgi_pass 127.0.0.1:5679;
              fastcgi_keep_conn on; 
	      #fastcgi_pass unix:/dev/shm/fcgi.sock; 
	      fastcgi_index index;
	      fastcgi_param GATEWAY_INTERFACE CGI/1.1;
	      fastcgi_param SCRIPT_FILENAME /mnt/sw/run/http/$fastcgi_script_name;
	      include fastcgi_params;
	}

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


}


stream {
    upstream backend2 {
        hash $remote_addr consistent;
	#### swConnect 1 or 2
        #server 172.119.0.3:8899 max_fails=10 fail_timeout=30s;
	#server 172.19.116.7:8899 max_fails=10 fail_timeout=30s;
        #server 172.19.10.3:9191 max_fails=2 fail_timeout=20s;
	#server 172.19.116.7:9191 max_fails=2 fail_timeout=20s;
        server 10.20.10.3:8899 max_fails=2 fail_timeout=20s;
        #server 10.20.10.5:7090 max_fails=2 fail_timeout=20s;
        #server unix:/tmp/backend3;
    }
 
    server {
        listen 7090;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass backend2;
    }
 
    upstream testBackend {
        hash $remote_addr consistent;
        server 127.0.0.1:7070 max_fails=10 fail_timeout=30s;
        #server unix:/tmp/backend3;
    }
 
    server {
        listen 20000;
        proxy_connect_timeout 10s;
        proxy_timeout 60s;
        proxy_pass testBackend;
    }

    #upstream testBackend2 {
    #    #hash $remote_addr consistent;
    #    server 110.20.10.3:19191 max_fails=2 fail_timeout=20s;
    #    #server unix:/tmp/backend3;
    #}
    #server {
    #    listen 30000;
    #    proxy_connect_timeout 10s;
    #    proxy_timeout 60s;
    #    proxy_pass testBackend2;
    #}

}

(1)Nginx参考链接
(2)Nginx官方文档

posted @ 2017-10-26 14:28  TuringM  阅读(274)  评论(0)    收藏  举报