Nginx命令及配置详解

1、nginx相关命令:
./nginx  启动nginx服务。
./nginx -s quit  等待工作进程完成当前请求的服务后才停止。
./nginx -s stop  快速停止。

./nginx -s reload  重新加载配置文件。

2、nginx配置详解
# 工作进程使用的用户和组。
user  root;
# 工作进程数,常设置为CPU核心数。
worker_processes  1;

# 错误日志存放路径。
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# nginx进程文件存放路径。
pid        logs/nginx.pid;

# 更改工作进程的最大打开文件数量限制。
worker_rlimit_nofile 65535;

events {
    # 工作进程可以打开的最大并发连接数,不能超过worker_rlimit_nofile数量。
    worker_connections  65535;
}


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

    # 设置读取客户端请求头的缓冲区大小,可以用命令 getconf PAGESIZE 取得。
    client_header_buffer_size  4k;
    large_client_header_buffers 4 32k;

    # 日志格式
    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    # 设置保持活动客户端连接在服务器端保持打开状态的超时时间。
    keepalive_timeout  65;
    # SSL连接、WebSocket代理需启用。
    tcp_nodelay on;

    #gzip  on;

    
    upstream mytomcat {
	# 影响请求分发有两种方式
	# 1、
        #round-robin; 以循环方式分发请求到服务器。
	#least-connected; 下一个请求被分配给活动连接数最少的服务器。
	#ip-hash; 以ip-hash确定下一个请求应该选择哪个服务器。
	# 2、
	# 配置权重。以下情况,假如接收到的3个请求,2个分配给8080服务器,1个分配到8090服务器。
        server 127.0.0.1:8080 weight=2;
        server 127.0.0.1:8090;
    }

    # server 模块可以有多个,这些模块通过它们侦听的端口和服务器名称来区分。
    server {
        # 监听80端口
        listen       80;
	# 用于确定该服务块响应哪些请求,多个server_name用空格隔开,例如 server_name  example.org  www.example.org;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	# 默认匹配,nginx会选择先最长前缀的块。
        location / {
	    # 对于匹配请求,URI将被添加到root指令中指定的路径,即/html/index.html,以形成本地文件系统上所请求文件的路径。
            #root   html;
            #index  index.html index.htm;

	    # 将客户端请求转到真实服务器上。
	    proxy_redirect off;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;

	    client_max_body_size 10m;
	    client_body_buffer_size 128k;
            # 使用了proxy_pass必须将proxy_redirect设置为off
            proxy_pass http://mytomcat;
        }

        # 静态资源直接读取
	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico|js|css|html)$
        {         
           root /home/tomcat/apache-tomcat-8.5.30/webapps/ROOT;
           expires 2d;
        }


        #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;
    #    }
    #}

}

猜你喜欢

转载自blog.csdn.net/lercent/article/details/80463187