Nginx configuration file modifications

1, in order to make Nginx start the service, stop, reload and other operations more convenient, you can write Nginx service script, and use the chkconfig and systemctl tools to manage, but also more in line with RHEL system management habits.

[root@nginx~]# vim /etc/init.d/nginx

#!/bin/bash

# chkconfig: 2345 99 20

# description: Nginx Server Control Script

PROG="/usr/local/nginx/sbin/nginx"

PIDF="/usr/local/nginx/logs/nginx.pid"

 

case "$1" in

start)

$PROG

;;

stop)

kill -s QUIT $(cat $PIDF)

;;

restart)

$0 stop

$0 start

;;

reload)

kill -s HUP $(cat $PIDF)

;;

*)

echo "Usage: $0 {start|stop|restart|reload}"

exit 1

esac

exit 0

 Save and exit

[root@nginx~]#chmod +x /etc/init.d/nginx

[root@nginx~]#chkconfig --add nginx

[root@nginx~]#chkconfignginx on

[root@nginx~]#chkconfig --list nginx

Nginx 0: Close 1: Close 2: Enabled 3: Enabled 4: Enabled 5: Enable 6: Close

 

This can nginx to start the script, stop, restart, reload Nginx server up.

2, nginx.conf file structure

In Nginx the main configuration file server nginx.conf , including global configuration, the I / O event configuration, HTTP Configuration three blocks content, format configuration statement is " keyword  value; " ( at the end with a semicolon indicates the end ) , with "#" section that starts with a comment.

1 ) Global Configuration

 Statements of various configurations, without the use of a particular marker definition. Global Configuration section running user, the number of work processes, error logs, including PID basic settings storage location.

 

Common configuration items:

  • user nginx [nginx]; // run user, the Nginx running the user actually specified a compile time Nginx , at compile time if not specified, the default is the nobody .
  • worker_processes 2; // specify nginx number of work processes initiated, as recommended by the cpu to specify the number of general and CPU cores equal.
  • worker_cpu_affinity 00000001 00000010; // allocate for each process cpu core, in the above example 2 of processes assigned to the two cpu , of course, one could write, or assign to a plurality of process cpu
  • worker_rlimit_nofile 102400; // this is when a command nginx maximum number of open files of the process, the theoretical value should be opened up to the number (document the ulimit-n- ) with nginx divided by the number of processes, but nginx allocation request is not so uniform, most good and ulimit -n consistent value. ( By "ulimit-n values " can modify the maximum number of open files )
  • error_log logs / error.log; // global error log files
  • pid logs / nginx.pid; // PID file location

2 ) the I / O event configuration:

Use "events {}" defines flag for specifying Nginx process I / O response model, the number of connection settings and the like for each process

events {

  // use epoll model, for 2.6 more than the kernel, it is recommended to use epoll model to improve performance

 

worker_connections 4096; // each process to allow the maximum number of connections ( default is 1024) , the number of connections per process should be based on actual needs, generally 10,000 or less, in theory, each nginx maximum number of connections the server is worker_processes * worker_connections , depends on the specific server hardware, bandwidth, and so on.

 

}

3 ) HTTP Configuration

 

Use "http {}" define the mark, including access logs, HTTP port, web directory, the default character set, keep the connection to and web hosting, PHP parsing a series of settings. Most of the configuration defining statement contained in the sub-mark "server {}" inside.

 

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 logs / access.log main; // access log-bit

 

sendfile on; // support files to send (download)

 

keepalive_timeout 65; // connector holding timeout

 

    server {// web service listener configuration

 

        listen 80; // listen address and port ( IP : PORT )

 

server_name   www.crushlinux.com ; // site name ( the FQDN of the )

 

charset utf-8; // page the default character set

    location / {// configured with directory

 

            root html; // location of the installation location of the web root of html in

 

            index index.html index.htm; // default home page (index page)

 

        }

 

error_page 500 502 503 504 /50x.html; // internal error feedback page

 

        location = /50x.html {// error page configuration

 

            root   html;

 

        }

 

 

3, the state statistics module

 

Nginx built HTTP_STUB_STATUS state statistics module, used to feed the current WEB visits. Configuration can be added when compiling the parameter --with-http_stub_stastus_module to enable this module. To use Nginx state statistical functions, in addition to the built-enabled module, also you need to modify nginx.conf file, specify the location and open access stub_status configuration. In http {} configuration server {} add the following configuration items arranged within the sub

 

[root@nginx~]# vim /usr/local/nginx/conf/nginx.conf

 

location /status {

 

ON stub_status; // open state statistics

 

OFF access_log; // Close logging this position

 

        }

 

[root@nginxconf]#systemctl restart nginx

 

 Browser access  http://192.168.200.111/status

 

 

 

Active connections represents the number of connections currently active,

The third row of three numbers represent Nginx is currently processing a total of 3 connections, successfully created 3 -way handshake, it handled a total of 12 requests.

Reading represents Nginx read to the client Header number information,

Writing represent Nginx returned to the client the Header count information

Waiting represents Nginx has been processed, the number of resident awaiting connection request command once.

4, the virtual host application

 Use Nginx When setting up a virtual host server, each virtual WEB site has a separate "server {}" configuration section, each listening IP address and port number can be specified individually, of course, the site name is different.

 

For example: To create two sites www.crushlinux.com and www.cloud.com

 

Two virtual WEB hosts were established root directory, and is ready to test Home

 

[root@nginx~]#mkdir /usr/local/nginx/html/crushlinux

 

[root@nginx~]#mkdir /usr/local/nginx/html/cloud

 

[root@nginx~]# echo "<h1>www.crushlinux.com</h1>" >/usr/local/nginx/html/crushlinux/index.html

 

[root@nginx~]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html

 

[root@nginx~]# vim /usr/local/nginx/conf/nginx.conf

 

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  logs/access.log  main;

 

sendfile        on;

 

keepalive_timeout  65;

 

    server {

 

        listen       80;

 

server_name  www.crushlinux.com;

 

charset utf-8;

 

access_log  logs/crushlinux.access.log  main;

 

        location / {

 

            root   html/crushlinux;

 

index  index.html index.htm;

 

        }

 

    }

 

 

    server {

 

        listen       80;

 

server_name  www.cloud.com;

 

        charset utf-8;

 

access_log  logs/cloud.access.log  main;

 

        location / {

 

            root   html/cloud;

 

index  index.html index.htm;

 

        }

 

    }

 

}

 

 

[root@nginx~]#systemctl restart nginx

 

 

[root@nginx~]# vim /etc/hosts

 

192.168.200.111 www.crushlinux.com

 

192.168.200.111 www.cloud.com

 

Virtual host access test

 

[root@nginx~]#elinks --dump http://www.crushlinux.com

 

                                 www.crushlinux.com

 

[root@nginx~]#elinks --dump http://www.cloud.com

 

                                  www.cloud.com

 

7, test

Respectively, enter the domain name and IP address in a browser to view the contents of the corresponding page

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/elin989898/p/11870632.html