nginx實戰

nginx介紹

http://nginx.org/php

nginx安裝(yum) 

https://coding.net/u/aminglinux/p/nginx/git/blob/master/2z/nginx.repocss

1.安裝最新穩定版本,能夠安裝epel-release擴展源,可是用epel安裝的這個版本比較老,咱們這裏用yum nignx源安裝,建立一個nginx yum源html

[root@centos-03 ~]# vim /etc/yum.repos.d/nginx.repo^C
[root@centos-03 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx] 
name=nginx repo 
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ 
gpgcheck=0 
enabled=1
[root@centos-03 ~]# 

2.用yum list命令查看下nginx的版本,若是有epel的源,爲了不產生衝突咱們將epel源先關掉linux

[root@centos-03 ~]# mv /etc/yum.repos.d/epel.repo epel.repo.bak

3.咱們以前安裝過nginx包先卸載掉nginx

[root@centos-03 ~]# yum remove nginx-filesystem
[root@centos-03 ~]# yum list | grep nginx      
nginx.x86_64                                1:1.14.0-1.el7_4.ngx       nginx    
nginx-debug.x86_64                          1:1.8.0-1.el7.ngx          nginx    
nginx-debuginfo.x86_64                      1:1.14.0-1.el7_4.ngx       nginx    
nginx-module-geoip.x86_64                   1:1.14.0-1.el7_4.ngx       nginx    
nginx-module-geoip-debuginfo.x86_64         1:1.14.0-1.el7_4.ngx       nginx    
nginx-module-image-filter.x86_64            1:1.14.0-1.el7_4.ngx       nginx    
nginx-module-image-filter-debuginfo.x86_64  1:1.14.0-1.el7_4.ngx       nginx    
nginx-module-njs.x86_64                     1:1.14.0.0.2.2-1.el7_4.ngx nginx    
nginx-module-njs-debuginfo.x86_64           1:1.14.0.0.2.2-1.el7_4.ngx nginx    
nginx-module-perl.x86_64                    1:1.14.0-1.el7_4.ngx       nginx    
nginx-module-perl-debuginfo.x86_64          1:1.14.0-1.el7_4.ngx       nginx    
nginx-module-xslt.x86_64                    1:1.14.0-1.el7_4.ngx       nginx    
nginx-module-xslt-debuginfo.x86_64          1:1.14.0-1.el7_4.ngx       nginx    
nginx-nr-agent.noarch                       2.0.0-12.el7.ngx           nginx    
pcp-pmda-nginx.x86_64                       3.12.2-5.el7               base     
[root@centos-03 ~]# 

4.開始yum安裝nginxgit

[root@centos-03 ~]# yum install -y nginx

5.啓動nginxweb

[root@centos-03 ~]# systemctl start nginx
[root@centos-03 ~]# ps aux|grep nginx
root       2702  0.0  0.0  48524   972 ?        Ss   23:46   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      2703  0.0  0.1  48928  1924 ?        S    23:46   0:00 nginx: worker process
root       2705  0.0  0.0 112660   964 pts/0    R+   23:46   0:00 grep --color=auto nginx
[root@centos-03 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2702/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1419/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2521/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1419/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2521/master         
[root@centos-03 ~]# 

6.curl訪問nginx算法

[root@centos-03 ~]# curl 192.168.242.133
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@centos-03 ~]# 

7.瀏覽器訪問失敗緣由是服務器開啓了iptables規則,關掉防火牆或者放行80端口vim

[root@centos-03 ~]# iptables -nvL
[root@centos-03 ~]# systemctl stop firewalld
[root@centos-03 ~]# iptables -nvL           
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@centos-03 ~]# 
[root@centos-03 ~]# systemctl stop iptables
[root@centos-03 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@centos-03 ~]#

8.查看nginx版本centos

[root@centos-03 ~]# nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-
log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/
nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx
--group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_
module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module
--with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module
--with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4
-grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' [root@centos-03 ~]#   

總結:首先配置一個nginx yum源,yum install安裝,啓動

nginx安裝(源碼)

http://nginx.org/en/download.html

1.下載源碼包

[root@centos-03 ~]# cd /usr/local/src/
[root@centos-03 src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
--2018-07-25 00:04:17--  http://nginx.org/download/nginx-1.14.0.tar.gz
正在解析主機 nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3, ...
正在鏈接 nginx.org (nginx.org)|95.211.80.227|:80... 已鏈接。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:1016272 (992K) [application/octet-stream]
正在保存至: 「nginx-1.14.0.tar.gz」

100%[======================================================================================>] 1,016,272    244KB/s 用時 4.1s   

2018-07-25 00:04:22 (244 KB/s) - 已保存 「nginx-1.14.0.tar.gz」 [1016272/1016272])

[root@centos-03 src]# ls
filebeat-6.3.1-x86_64.rpm  nginx-1.14.0.tar.gz
[root@centos-03 src]# 
[root@centos-03 src]# du -sh nginx-1.14.0.tar.gz 
996K    nginx-1.14.0.tar.gz
[root@centos-03 src]# 

2.解壓tar包

[root@centos-03 src]# tar zxvf nginx-1.14.0.tar.gz 

3.編譯安裝能夠查看配置參數

[root@centos-03 src]# ls
filebeat-6.3.1-x86_64.rpm  nginx-1.14.0  nginx-1.14.0.tar.gz
[root@centos-03 src]# cd nginx-1.14.0
[root@centos-03 nginx-1.14.0]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_mirror_module       disable ngx_http_mirror_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_grpc_module         disable ngx_http_grpc_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

[root@centos-03 nginx-1.14.0]# 

4.src下面爲nginx的核心源碼文件

[root@centos-03 nginx-1.14.0]# cd src/
[root@centos-03 src]# ls
core  event  http  mail  misc  os  stream
[root@centos-03 src]# 
[root@centos-03 src]# ls core/
nginx.c           ngx_core.h     ngx_hash.c    ngx_module.h           ngx_parse_time.h      ngx_resolver.c  ngx_string.c
nginx.h           ngx_cpuinfo.c  ngx_hash.h    ngx_murmurhash.c       ngx_proxy_protocol.c  ngx_resolver.h  ngx_string.h
ngx_array.c       ngx_crc32.c    ngx_inet.c    ngx_murmurhash.h       ngx_proxy_protocol.h  ngx_rwlock.c    ngx_syslog.c
ngx_array.h       ngx_crc32.h    ngx_inet.h    ngx_open_file_cache.c  ngx_queue.c           ngx_rwlock.h    ngx_syslog.h
ngx_buf.c         ngx_crc.h      ngx_list.c    ngx_open_file_cache.h  ngx_queue.h           ngx_sha1.c      ngx_thread_pool.c
ngx_buf.h         ngx_crypt.c    ngx_list.h    ngx_output_chain.c     ngx_radix_tree.c      ngx_sha1.h      ngx_thread_pool.h
ngx_conf_file.c   ngx_crypt.h    ngx_log.c     ngx_palloc.c           ngx_radix_tree.h      ngx_shmtx.c     ngx_times.c
ngx_conf_file.h   ngx_cycle.c    ngx_log.h     ngx_palloc.h           ngx_rbtree.c          ngx_shmtx.h     ngx_times.h
ngx_config.h      ngx_cycle.h    ngx_md5.c     ngx_parse.c            ngx_rbtree.h          ngx_slab.c
ngx_connection.c  ngx_file.c     ngx_md5.h     ngx_parse.h            ngx_regex.c           ngx_slab.h
ngx_connection.h  ngx_file.h     ngx_module.c  ngx_parse_time.c       ngx_regex.h           ngx_spinlock.c
[root@centos-03 src]# 

5.編譯安裝,咱們只須要指定安裝目錄就能夠了,到用到什麼模塊時咱們再編譯安裝對應的模塊就能夠了

[root@centos-03 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
[root@centos-03 nginx-1.14.0]# yum install -y gcc
[root@centos-03 nginx-1.14.0]#  yum install -y pcre-devel 
[root@centos-03 nginx-1.14.0]# yum -y install zlib-devel

6.make

[root@centos-03 nginx-1.14.0]# make

7.make install

[root@centos-03 nginx-1.14.0]# make install
[root@centos-03 nginx-1.14.0]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@centos-03 nginx-1.14.0]# 

8.查看版本

[root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
configure arguments: --prefix=/usr/local/nginx
[root@centos-03 nginx-1.14.0]# 

9.啓動的時候咱們須要將yum安裝的nginx停掉

[root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx 
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
^C
[root@centos-03 nginx-1.14.0]# systemctl stop nginx
[root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx 
[root@centos-03 nginx-1.14.0]# ps aux|grep nginx
root       9203  0.0  0.0  20548   612 ?        Ss   00:32   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     9204  0.0  0.1  20992  1076 ?        S    00:32   0:00 nginx: worker process
root       9206  0.0  0.0 112724   976 pts/0    R+   00:33   0:00 grep --color=auto nginx
[root@centos-03 nginx-1.14.0]# 

10.查看下端口

[root@centos-03 nginx-1.14.0]# !net
netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9203/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1419/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2521/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1419/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2521/master         
[root@centos-03 nginx-1.14.0]# 

11.訪問下

[root@centos-03 nginx-1.14.0]# !curl
curl 192.168.242.133
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@centos-03 nginx-1.14.0]# 

12.結束進程能夠用killall nginx(或pkill),若是沒有killall命令用rpm命令查找須要安裝的包

[root@centos-03 nginx-1.14.0]# rpm -qf `which killall`
[root@centos-03 nginx-1.14.0]# yum install -y psmisc
[root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx 
[root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 nginx-1.14.0]# 

13.nginx服務管理腳本

https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx

[root@centos-03 nginx-1.14.0]# vim /etc/init.d/nginx^C
[root@centos-03 nginx-1.14.0]# cat /etc/init.d/nginx 
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}

stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}

reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart()
{
    stop
    start
}

configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL

[root@centos-03 nginx-1.14.0]# 

14.咱們先關掉nginx,而後用腳本啓動,發現有衝突啓動的是yum安裝的那個nginx,咱們先把yum安裝的卸載掉

[root@centos-03 nginx-1.14.0]# killall nginx
nginx: no process found
[root@centos-03 nginx-1.14.0]# chmod 755 /etc/init.d/nginx 
[root@centos-03 nginx-1.14.0]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  肯定  ]
[root@centos-03 nginx-1.14.0]# !ps
ps aux|grep nginx
root       9292  0.0  0.0  48580   968 ?        Ss   01:08   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      9293  0.0  0.1  48984  1672 ?        S    01:08   0:00 nginx: worker process
root       9295  0.0  0.0 112724   976 pts/0    R+   01:08   0:00 grep --color=auto nginx
[root@centos-03 nginx-1.14.0]# 
[root@centos-03 nginx-1.14.0]# yum remove -y nginx

15.再次成功啓動了咱們編譯安裝的nginx

[root@centos-03 nginx-1.14.0]# ps aux|grep nginx  
root       9320  0.0  0.0 112724   976 pts/0    S+   01:10   0:00 grep --color=auto nginx
[root@centos-03 nginx-1.14.0]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  肯定  ]
[root@centos-03 nginx-1.14.0]# ps aux|grep nginx      
root       9338  0.0  0.0  20548   604 ?        Ss   01:10   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     9339  0.0  0.1  20988  1068 ?        S    01:10   0:00 nginx: worker process
root       9341  0.0  0.0 112724   976 pts/0    R+   01:10   0:00 grep --color=auto nginx
[root@centos-03 nginx-1.14.0]# 

16.中止stop

[root@centos-03 nginx-1.14.0]# /etc/init.d/nginx stop
Stopping nginx (via systemctl):                            [  肯定  ]
[root@centos-03 nginx-1.14.0]# ps aux|grep nginx     
root       9359  0.0  0.0 112724   976 pts/0    R+   01:12   0:00 grep --color=auto nginx
[root@centos-03 nginx-1.14.0]# 

17.開機啓動nginx

[root@centos-03 nginx-1.14.0]# chkconfig --list

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

      要列出 systemd 服務,請執行 'systemctl list-unit-files'。
      查看在具體 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole      0:關    1:關    2:關    3:關    4:關    5:關    6:關
network         0:關    1:關    2:開    3:開    4:開    5:開    6:關
[root@centos-03 nginx-1.14.0]# chkconfig --add nginx
[root@centos-03 nginx-1.14.0]# chkconfig --list     

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

      要列出 systemd 服務,請執行 'systemctl list-unit-files'。
      查看在具體 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole      0:關    1:關    2:關    3:關    4:關    5:關    6:關
network         0:關    1:關    2:開    3:開    4:開    5:開    6:關
nginx           0:關    1:關    2:關    3:關    4:關    5:關    6:關
[root@centos-03 nginx-1.14.0]# chkconfig nginx on
[root@centos-03 nginx-1.14.0]# chkconfig --list  

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

      要列出 systemd 服務,請執行 'systemctl list-unit-files'。
      查看在具體 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole      0:關    1:關    2:關    3:關    4:關    5:關    6:關
network         0:關    1:關    2:開    3:開    4:開    5:開    6:關
nginx           0:關    1:關    2:開    3:開    4:開    5:開    6:關
[root@centos-03 nginx-1.14.0]# 

nginx配置文件詳解 

https://coding.net/u/aminglinux/p/nginx/git/tree/master/3z

1.編譯安裝的配置文件路徑

[root@centos-03 nginx-1.14.0]# ls /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
[root@centos-03 nginx-1.14.0]# 

2.yum安裝配置文件路徑

[root@centos-03 nginx-1.14.0]# ls /etc/nginx/

3.nginx配置文件說明

  oot@centos-03 nginx-1.14.0]# cd /usr/local/nginx/conf/
▽root@centos-03 conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@centos-03 conf]# vim nginx.conf
[root@centos-03 conf]# cat nginx.conf

#user  nobody;     (以哪一個用戶運行worker進程,定義運行nginx服務的用戶,還能夠加上組,如 user nobody nobody;)
worker_processes  1; (定義worker進程有幾個,這個數對應cpu個數,定義nginx子進程數量,即提供服務的進程數量,該數值建議和服務cpu核數保持一致。 除了能夠定義數字外,還能夠定義爲auto,表示讓系統自動調整。)

#error_log  logs/error.log; (定義錯誤日誌的路徑,能夠是相對路徑(相對prefix路徑的),也能夠是絕對路徑。 該配置能夠在此處定義,也能夠定義到http、server、location裏)
#error_log  logs/error.log  notice;(定義錯誤日誌路徑以及日誌級別. 錯誤日誌級別:常見的錯誤日誌級別有[debug|info|notice|warn|error|crit|alert|emerg],級別越高記錄的信息越少。 若是不定義默認是error)
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; (定義nginx進程pid文件所在路徑,能夠是相對路徑,也能夠是絕對路徑。)


events {
    worker_connections  1024; (定義每一個work_process同時開啓的最大鏈接數,即容許最多隻能有這麼多鏈接)

worker_rlimit_nofile 100000; (定義nginx最多打開文件數限制。若是沒設置的話,這個值爲操做系統(ulimit -n)的限制保持一致。 把這個值設高,nginx就不會有「too many open files」問題了。)

accept_mutex on;(當某一個時刻只有一個網絡鏈接請求服務器時,服務器上有多個睡眠的進程會被同時叫醒,這樣會損耗必定的服務器性能。 Nginx中的accept_mutex設置爲on,將會對多個Nginx進程(worker processer)接收鏈接時進行序列化,防止多個進程爭搶資源。 默認就是on。)

multi_accept on;(nginx worker processer能夠作到同時接收多個新到達的網絡鏈接,前提是把該參數設置爲on。 默認爲off,即每一個worker process一次只能接收一個新到達的網絡鏈接。

use epoll;(Nginx服務器提供了多個事件驅動器模型來處理網絡消息。 其支持的類型有:select、poll、kqueue、epoll、rtsing、/dev/poll以及eventport。 * select:只能在Windows下使用,這個事件模型不建議在高負載的系統使用 * poll:Nginx默認首選,但不是在全部系統下均可用 * kqueue:這種方式在FreeBSD 4.1+, OpenBSD2.9+, NetBSD 2.0, 和 MacOS X系統中是最高效的 * epoll: 這種方式是在Linux 2.6+內核中最高效的方式 * rtsig:實時信號,可用在Linux 2.2.19的內核中,但不適用在高流量的系統中 * /dev/poll: Solaris 7 11/99+,HP/UX 11.22+, IRIX 6.5.15+, and Tru64 UNIX 5.1A+操做系統最高效的方式 * eventport: Solaris 10最高效的方式

}

 

http {
    include       mime.types; (include mime.types; //cat conf/mime.types 定義nginx能識別的網絡資源媒體類型(如,文本、html、js、css、流媒體等) default_type application/octet-stream; 定義默認的type,
若是不定義改行,默認爲text/plain.
) default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' (log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent

"$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; 其中main爲日誌格式的名字,後面的爲nginx的內部變量組成的一串字符串。
) # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; (定義日誌的路徑以及採用的日誌格式,該參數能夠在server配置塊中定義。) sendfile on; (是否調用sendfile函數傳輸文件,默認爲off,使用sendfile函數傳輸,能夠減小user mode和kernel mode的切換,從而提高服務器性能。 對於普通應用設爲 on,若是用來進行下載等應用磁盤IO重負載應用,
可設置爲off,以平衡磁盤與網絡I/O處理速度,下降系統的負載。

sendfile_max_chunk 128k;(該參數限定Nginx worker process每次調用sendfile()函數傳輸數據的最大值,默認值爲0,若是設置爲0則無限制

    #tcp_nopush     on; (當tcp_nopush設置爲on時,會調用tcp_cork方法進行數據傳輸。 使用該方法會產生這樣的效果:當應用程序產生數據時,內核不會立馬封裝包,而是當數據量積累到必定量時纔會封裝,而後傳輸。這樣有助於解決網絡
堵塞問題。 默認值爲on。舉例:快遞員收快遞、發快遞,包裹累積到必定量纔會發,節省運輸成本。
) #keepalive_timeout 0; keepalive_timeout 65; (該參數有兩個值,第一個值設置nginx服務器與客戶端會話結束後仍舊保持鏈接的最長時間,單位是秒,默認爲75s。 第二個值能夠省略,它是針對客戶端的瀏覽器來設置的,能夠經過curl -I看到header信息中有一項
Keep-Alive: timeout=60,若是不設置就沒有這一項。 第二個數值設置後,瀏覽器就會根據這個數值決定什麼時候主動關閉鏈接,Nginx服務器就不操心了。但有的瀏覽器並不承認該參數。

send_timeout(這個超時時間是發送響應的超時時間,即Nginx服務器向客戶端發送了數據包,但客戶端一直沒有去接收這個數據包。 若是某個鏈接超過send_timeout定義的超時時間,那麼Nginx將會關閉這個鏈接。)

client_max_body_size 10m;(瀏覽器在發送含有較大HTTP包體的請求時,其頭部會有一個Content-Length字段,client_max_body_size是用來限制Content-Length所示值的大小的。 這個限制包體的配置不用等Nginx接收完全部的HTTP包體,就能夠告訴用戶請求過大不被接受。會返回413狀態碼。 例如,用戶試圖上傳一個1GB的文件,Nginx在收完包頭後,發現Content-Length超過client_max_body_size定義的值, 就直接發送413(Request Entity Too Large)響應給客戶端。

    #gzip  on; (是否開啓gzip壓縮。)

    server {
        listen       80; (//監聽端口爲80,能夠自定義其餘端口,也能夠加上IP地址,如,listen 127.0.0.1:8080;)
        server_name  localhost; ( //定義網站域名,能夠寫多個,用空格分隔。)

        #charset koi8-r; (//定義網站的字符集,通常不設置,而是在網頁代碼中設置。)

        #access_log  logs/host.access.log  main; (//定義訪問日誌,能夠針對每個server(即每個站點)設置它們本身的訪問日誌。)

        location / {
            root   html; (//定義網站根目錄,目錄能夠是相對路徑也能夠是絕對路徑。)
            index  index.html index.htm; (//定義站點的默認頁。)
        }

        #error_page  404              /404.html; (//定義404頁面 )

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html; (//當狀態碼爲500、50二、50三、504時,則訪問50x.html)
        location = /50x.html {
            root   html; (/定義50x.html所在路徑)
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ { (定義訪問php腳本時,將會執行本location{}部分指令)
        #    proxy_pass   http://127.0.0.1; (//proxy_pass後面指定要訪問的url連接,用proxy_pass實現代理)
        #}

        # 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服務器監聽端口與地址,支持兩種形式,1 IP:Port, 2 unix:/path/to/sockt)
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; (//定義SCRIPT_FILENAME變量,後面的路徑/scripts爲上面的root指定的目錄)
        #    include        fastcgi_params; (//引用prefix/conf/fastcgi_params文件,該文件定義了fastcgi相關的變量)
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all; (//訪問的url中,以/.ht開頭的,如,www.example.com/.htaccess,會被拒絕,返回403狀態碼。)
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000; (//監聽8000端口)
    #    listen       somename:8080; (//指定ip:port)
    #    server_name  somename  alias  another.alias; (//指定多個server_name)

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl; (//監聽443端口,即ssl)
    #    server_name  localhost;

    #    ssl_certificate      cert.pem; (//指定pem文件路徑)
    #    ssl_certificate_key  cert.key; (//指定key文件路徑)

    #    ssl_session_cache    shared:SSL:1m; (//指定session cache大小)
    #    ssl_session_timeout  5m; (/指定session超時時間)

    #    ssl_ciphers  HIGH:!aNULL:!MD5; (/指定ssl算法)
    #    ssl_prefer_server_ciphers  on;

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

}
[root@centos-03 conf]# 
相關文章
相關標籤/搜索