nginx总结+Centos7 防火墙命令操作+nginx编译安装1.14版本
1、main全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。 2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。 3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。 4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。 5、location块:配置请求的路由,以及各种页面的处理情况。 不同模块指令关系:server继承main;location继承server;upstream既不会继承指令也不会被继承,它有自己的特殊指令,不需要在其他地方的应用 nginx.conf 基本配置模板 每个指令必须有分号结束 ########### 每个指令必须有分号结束。################# #配置用户或者组,默认为nobody nobody。 #user administrator administrators; #允许生成的进程数,默认为1 #worker_processes 2; #指定nginx进程运行文件存放地址 #pid /nginx/pid/nginx.pid; #制定错误日志路径,级别。这个设置可以放入全局块,http块,server块,级别依次为:debug|info|notice|warn|error|crit|alert|emerg error_log log/error.log debug; #工作模式及连接数上限 events { #设置网路连接序列化,防止惊群现象发生,默认为on accept_mutex on; #设置一个进程是否同时接受多个网络连接,默认为off multi_accept on; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport #use epoll; #单个work进程允许的最大连接数,默认为512 worker_connections 1024; } #http服务器 http { #文件扩展名与文件类型映射表。设定mime类型(邮件支持类型),类型由mime.types文件定义 #include /usr/local/etc/nginx/conf/mime.types; include mime.types; #默认文件类型,默认为text/plain default_type application/octet-stream; #取消服务访问日志 #access_log off; #自定义日志格式 log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #设置访问日志路径和格式。"log/"该路径为nginx日志的相对路径,mac下是/usr/local/var/log/。combined为日志格式的默认值 access_log log/access.log myFormat; rewrite_log on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。(sendfile系统调用不需要将数据拷贝或者映射到应用程序地址空间中去) sendfile on; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。 sendfile_max_chunk 100k; #连接超时时间,默认为75s,可以在http,server,location块。 keepalive_timeout 65; #gzip压缩开关 #gzip on; tcp_nodelay on; #设定实际的服务器列表 upstream mysvr1 { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; #热备(其它所有的非backup机器down或者忙的时候,请求backup机器)) } upstream mysvr2 { #weigth参数表示权值,权值越高被分配到的几率越大 server 192.168.1.11:80 weight=5; server 192.168.1.12:80 weight=1; server 192.168.1.13:80 weight=6; } upstream https-svr { #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题 ip_hash; server 192.168.1.11:90; server 192.168.1.12:90; } #error_page 404 https://www.baidu.com; #错误页 #HTTP服务器 # 静态资源一般放在nginx所在主机 server { listen 80; #监听HTTP端口 server_name 127.0.0.1; #监听地址 keepalive_requests 120; #单连接请求上限次数 set $doc_root_dir "/Users/doing/IdeaProjects/edu-front-2.0"; #设置server里全局变量 #index index.html; #定义首页索引文件的名称 location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。 root $doc_root_dir; #静态资源根目录 proxy_pass http://mysvr1; #请求转向“mysvr1”定义的服务器列表 #deny 127.0.0.1; #拒绝的ip #allow 172.18.5.54; #允许的ip } } #http server { listen 80; server_name www.helloworld.com; #监听基于域名的虚拟主机。可有多个,可以使用正则表达式和通配符 charset utf-8; #编码格式 set $static_root_dir "/Users/doing/static"; location /app1 { #反向代理的路径(和upstream绑定),location后面设置映射的路径 proxy_pass http://zp_server1; } location /app2 { proxy_pass http://zp_server2; } location ~ ^/(images|javascript|js|css|flash|media|static)/ { #静态文件,nginx自己处理 root $static_root_dir; expires 30d; #静态资源过时间30天 } location ~ /\.ht { #禁止访问 .htxxx 文件 deny all; } location = /do_not_delete.html { #直接简单粗暴的返回状态码及内容文本 return 200 "hello."; } # 指定某些路径使用https访问(使用正则表达式匹配路径+重写uri路径) location ~* /http* { #路径匹配规则:如localhost/http、localhost/httpsss等等 #rewrite只能对域名后边的除去传递的参数外的字符串起作用,例如www.c.com/proxy/html/api/msg?method=1¶=2只能对/proxy/html/api/msg重写。 #rewrite 规则 定向路径 重写类型; #rewrite后面的参数是一个简单的正则。$1代表正则中的第一个()。 #$host是nginx内置全局变量,代表请求的主机名 #重写规则permanent表示返回301永久重定向 rewrite ^/(.*)$ https://$host/$1 permanent; } #错误处理页面(可选择性配置) #error_page 404 /404.html; #error_page 500 502 503 504 /50x.html; #以下是一些反向代理的配置(可选择性配置) #proxy_redirect off; #proxy_set_header Host $host; #proxy_set_header用于设置发送到后端服务器的request的请求头 #proxy_set_header X-Real-IP $remote_addr; #proxy_set_header X-Forwarded-For $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP #proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时) #proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) #proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) #proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 #proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 #proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) #proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 #client_max_body_size 10m; #允许客户端请求的最大单文件字节数 #client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 } #https #(1)HTTPS的固定端口号是443,不同于HTTP的80端口; #(2)SSL标准需要引入安全证书,所以在 nginx.conf 中你需要指定证书和它对应的 key server { listen 443; server_name www.hellohttps1.com www.hellohttps2.com; set $geek_web_root "/Users/doing/IdeaProjects/backend-geek-web"; ssl_certificate /usr/local/etc/nginx/ssl-key/ssl.crt; #ssl证书文件位置(常见证书文件格式为:crt/pem) ssl_certificate_key /usr/local/etc/nginx/ssl-key/ssl.key; #ssl证书key位置 location /passport { send_timeout 90; proxy_connect_timeout 50; proxy_send_timeout 90; proxy_read_timeout 90; proxy_pass http://https-svr; } location ~ ^/(res|lib)/ { root $geek_web_root; expires 7d; #add_header用于为后端服务器返回的response添加请求头,这里通过add_header实现CROS跨域请求服务器 add_header Access-Control-Allow-Origin *; } #ssl配置参数(选择性配置) ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; } #配置访问控制:每个IP一秒钟只处理一个请求,超出的请求会被delayed #语法:limit_req_zone $session_variable zone=name:size rate=rate (为session会话状态分配一个大小为size的内存存储区,限制了每秒(分、小时)只接受rate个IP的频率) limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s nodelay; location /pay { proxy_set_header Host $http_host; proxy_set_header X-Real_IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #访问控制:limit_req zone=name [burst=number] [nodelay]; limit_req zone=req_one burst=5; #burst=5表示超出的请求(被delayed)如果超过5个,那些请求会被终止(默认返回503) proxy_pass http://mysvr1; } #可以把子配置文件放到/usr/local/etc/nginx/servers/路径下,通过include引入 include /usr/local/etc/nginx/servers/*.conf; } 内置全局变量: $args :这个变量等于请求行中的参数,同$query_string $content_length : 请求头中的Content-length字段。 $content_type : 请求头中的Content-Type字段。 $document_root : 当前请求在root指令中指定的值。 $host : 请求主机头字段,否则为服务器名称。 $http_user_agent : 客户端agent信息 $http_cookie : 客户端cookie信息 $limit_rate : 这个变量可以限制连接速率。 $request_method : 客户端请求的动作,通常为GET或POST。 $remote_addr : 客户端的IP地址。 $remote_port : 客户端的端口。 $remote_user : 已经经过Auth Basic Module验证的用户名。 $request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。 $scheme : HTTP方法(如http,https)。 $server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。 $server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。 $server_name : 服务器名称。 $server_port : 请求到达服务器的端口号。 $request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。 $uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。 $document_uri : 与$uri相同。 如何验证效果 如何确定就是本地 nginx 提供的服务呢?有 2 个方法: 方法一:PING ping ip,看看是否解析到设置的url,比如http://baidu.com等 方法二:观察访问日志 查看nginx 服务器的访问日志,看nginx 服务器是否能够正常提供服务。
如上是nginx参数详解,以备回顾;
#user nobody; worker_processes 3; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 128k; fastcgi_buffers 8 128k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; #gzip on; server_names_hash_bucket_size 64; server { listen 80; server_name ""; return 444; } server { listen 80; server_name server1.gdsf.gov.cn; location / { proxy_pass http://ip1:9000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 50M; } } server { listen 80; server_name server2.gdsf.gov.cn; location / { proxy_pass http://ip2:9000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 50M; } } }
访问不同的域名就会跳转到不同的应用服务器对应的应用了!
把配置文件拆分开来,在/etc/nginx/conf.d/ 文件建立对应的域名配置文件,比如 /etc/nginx/conf.d/123.com.conf
我们安装了nginx后发现配置文件只有一个,/etc/nginx/nginx.conf 所有的配置包括虚拟目录也在此文件中配置, 这样当虚拟主机多了管理就有些不方便了, 这是需要我们把配置文件拆分开来,在/etc/nginx/conf.d/ 文件建立对应的域名配置文件,比如 /etc/nginx/conf.d/123.com.conf 怎么配置呢? 只需要在原来文件/etc/nginx/nginx.conf 的http 块下加一句话就可以了: include /etc/nginx/conf.d/*.conf; /etc/nginx/nginx.conf完整的代码: #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #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 $document_root$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; # } #} include /etc/nginx/conf.d/*.conf; } 之后就可以把虚拟主机的配置文件写在/etc/nginx/conf.d/目录下了,如123.com.conf server { listen 80; server_name 123.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /var/nginx/html/123.com; 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 /var/nginx/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 /var/nginx/html/123.com; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$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; #} }
动态代理 动态代理就是把代理服务器的请求转发到另一个服务上去,这里我们将对api.macrozheng.com的请求代理到mall-admin的后台服务上去。 首先我们修改下本机的host文件,添加如下规则: 192.168.6.132 api.macrozheng.com 在/mydata/nginx/conf/conf.d文件夹中添加配置文件api.conf对将请求代理到远程的mall-admin服务上去: server { listen 80; server_name api.macrozheng.com; #修改域名 location / { proxy_pass http://120.27.63.9:8080; #修改为代理服务地址 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } 重启动nginx服务后,通过api.macrozheng.com/swagger-ui.html即可访问到mall-admin的API文档页面了: 文件压缩 如果我们租用了一个带宽很低的服务器,网站访问速度会很慢,这时我们可以通过让nginx开启GZIP压缩来提高网站的访问速度。这里我们以mall的前端项目为例来演示下它的提速效果。 首先我们对nginx进行限速操作,限制每个连接的访问速度为128K来建立一个比较慢的访问场景; 修改mall.conf配置文件,进行限速操作: server { listen 80; server_name mall.macrozheng.com; limit_rate 128k; #限制网速为128K location / { root /usr/share/nginx/html/mall; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } 对mall的前端项目mall.macrozheng.com进行访问,我们可以发现网站中有个js文件比较大,需要加载12s: nginx返回请求头信息如下: 修改/mydata/nginx/conf目录下的nginx.conf配置文件,开启GZIP压缩; http { gzip on; #开启gzip gzip_disable "msie6"; #IE6不使用gzip gzip_vary on; #设置为on会在Header里增加 "Vary: Accept-Encoding" gzip_proxied any; #代理结果数据的压缩 gzip_comp_level 6; #gzip压缩比(1~9),越小压缩效果越差,但是越大处理越慢,所以一般取中间值 gzip_buffers 16 8k; #获取多少内存用于缓存压缩结果 gzip_http_version 1.1; #识别http协议的版本 gzip_min_length 1k; #设置允许压缩的页面最小字节数,超过1k的文件会被压缩 gzip_types application/javascript text/css; #对特定的MIME类型生效,js和css文件会被压缩 include /etc/nginx/conf.d/*.conf; } 再次对mall的前端项目mall.macrozheng.com进行访问,我们可以发现js文件已经被压缩,加载时间缩短到3.88s,提速3倍左右: nginx返回请求头中添加了Content-Encoding: gzip的信息: 地址重写 有的时候我们的网站更换了域名,但还有用户在使用老的域名访问,这时可以通过nginx的地址重写来让用户跳转到新的域名进行访问。 比如说原来我们用的docs.macrozheng.com这个域名不用了,现在改成www.macrozheng.com了来访问文档项目了; 修改docs.conf配置文件,将地址带参数重写到新地址: server { listen 80; server_name docs.macrozheng.com; rewrite "^/(.*)$" http://www.macrozheng.com/$1; #地址重写到新地址 location / { root /usr/share/nginx/html/docs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } 此时访问旧域名docs.macrozheng.com会直接跳转到www.macrozheng.com去。
nginx安装
默认情况下,Centos7防火墙是打开的,如果你没有关闭他,你安装nginx后启动,是无法访问到nginx服务的。
所以需要做这件事
1、启动nginx
启动前先看它启动没有,通过linux命令查看所有端口,看看有没有80
netstat -ntlp
如果没有,则通过命令启动
启动:systemctl start nginx
停止:systemctl stop nginx
重启:systemctl restart nginx
2、设置nginx开启启动
sudo chkconfig nginx on
3、关闭Centos自带的firewall防火墙
systemctl stop firewalld.service #停止firewall
4、禁止Firewall下次启动
systemctl disable firewalld.service #禁止firewall开机启动
其他命令
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?
查看已启动的服务列表:systemctl list-unit-files|grep enabled
Centos 7 firewall 命令:
查看已经开放的端口:
firewall-cmd --list-ports
开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
重启防火墙
firewall-cmd --reload #重启firewall
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
下面说下CentOS7和6的默认防火墙的区别
CentOS 7默认使用的是firewall作为防火墙,使用iptables必须重新设置一下
1、直接关闭防火墙
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
2、设置 iptables service
yum -y install iptables-services
如果要修改防火墙配置,如增加防火墙端口3306
vi /etc/sysconfig/iptables
增加规则
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
保存退出后
systemctl restart iptables.service #重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
最后重启系统使设置生效即可。
systemctl start iptables.service #打开防火墙
systemctl stop iptables.service #关闭防火墙 解决主机不能访问虚拟机CentOS中的站点 前阵子在虚拟机上装好了CentOS6.2,并配好了apache+php+mysql,但是本机就是无法访问。一直就没去折腾了。
具体情况如下
1. 本机能ping通虚拟机
2. 虚拟机也能ping通本机
3.虚拟机能访问自己的web
4.本机无法访问虚拟机的web
后来发现是防火墙将80端口屏蔽了的缘故。
检查是不是服务器的80端口被防火墙堵了,可以通过命令:telnet server_ip 80 来测试。
解决方法如下:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
然后保存:
/etc/rc.d/init.d/iptables save
重启防火墙
/etc/init.d/iptables restart
CentOS防火墙的关闭,关闭其服务即可:
查看CentOS防火墙信息:/etc/init.d/iptables status
关闭CentOS防火墙服务:/etc/init.d/iptables stop
ps:记得给服务器安全组开放端口
centos7----安装
这里安装的是nginx 1.14版本 1.下载源码 #下载 wget http://nginx.org/download/nginx-1.14.0.tar.gz #解压 tar -xzf nginx-1.14.0.tar.gz cd nginx-1.14.0 2.安装编译环境 yum update yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel 3.编译安装 #添加用户和组 groupadd www useradd -g www www #配置 ./configure \ --user=www \ --group=www \ --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-threads #编译 make #安装 make install 4.验证 /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) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads 5.创建软链接 ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx 6.开机自启动 vim /etc/init.d/nginx 输入如下内容 #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: NGINX is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac 赋予脚本可执行权限 chmod a+x /etc/init.d/nginx 将nginx服务加入chkconfig管理列表 chkconfig --add /etc/init.d/nginx chkconfig nginx on # 启动 nginx 7.测试 curl localhost:80 会有如下输出 <!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> 8.配置优化 下列配置在nginx.conf里与http节点同级 cd /usr/local/nginx/conf vim nginx.conf user www www; worker_processes auto; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } http节点下加入下列配置,将会从指定目录加载配置文件 include /etc/nginx/*.conf; 9.常用命令 # 启动 nginx # 停止 nginx -s stop # 重载配置 nginx -s reload # 测试配置是否正确 nginx -t 参考资料 https://www.cnblogs.com/visec479/p/5145624.html https://www.cnblogs.com/whatmiss/p/7091220.html
在/etc/nginx/*.conf;
新建配置文件在etc/nginx目录下,新建比如jd.com.conf即可,然后重新nginx -s reload
浙公网安备 33010602011771号