启动脚本javascript
nginx启动是用/usr/local/nginx/sbin/nginx,实际这样的话不太方便,因此须要给它作一个启动脚本。php
## 编写启动脚本css
[root@localhost ~]# vim /etc/init.d/nginxhtml
#!/bin/bash java
# chkconfig: - 30 21node
# description: http service.nginx
# Source Function Libraryapache
. /etc/init.d/functionsvim
# 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
解释说明:
nginx它并无本身的启动脚本,但咱们能够本身手动写一个
chkconfig: - 30 21 启动级别 启动顺序 关闭顺序
此行必须有(加入到启动列表中须要定义启动级别,与启动顺序关闭顺序)
## 修改权限
[root@localhost ~]# chmod 755 !$
chmod 755 /etc/init.d/nginx
## 加入启动列表中
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on
## 重启nginx
[root@localhost ~]# service nginx restart
配置文件
nginx自带的配置文件太乱了,咱们不须要这个自带的,须要把里面的内容清空,从新写一个。
## 重定向清空一下
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# > !$
> /usr/local/nginx/conf/nginx.conf
## 从新编辑
[root@localost ~]# vim /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 6000;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
include /usr/local/nginx/conf/vhosts/*.conf;
}
解释说明:
user 启动子进程的用户,而主进程必须是root,普通用户没权限监听80端口(1-1024只有root用户才能监听)
worker_processes 开启几个子进程
error_log 日志路径
crit 日志级别,最严谨的,获取的信息最少
pid 进程号存放路径
worker_rlimit_nofile 打开的文件描述符个数(通常设置的大一些)
events 配置使用的模型
use 默认使用epoll速度快,效率高
worker_connections 每一个worker能够支持的链接数
格式配置
http
server_names_hash_bucket_size 3526; 虚拟主机缓存3-4个256足够
日志格式
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
gzip on; 是否支持压缩
压缩参数
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
设置须要压缩的文件类型js css htm xml
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
## 建立vhosts目录
[root@localhost ~]# mkdir /usr/local/nginx/conf/vhosts
## 进入到vhost目录下,编辑具体的虚拟主机配置文件
[root@localhost ~]# cd !$
[root@localost vhosts]# vim default.conf
server
{
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root /tmp/1233;
deny all;
}
解释说明:
跟apache同样,也有一个默认虚拟主机;也就是你的主机无论解析什么样的域名绑定过来,都会走这个虚拟主机,那若是咱们为了限制它们,咱们应该把第一个默认的虚拟主机给它搞成403;就好比root(指定网站根目录)这写成/tmp/1233,通常状况下,不会在/tmp/1233下写东西,若是写了,就加上deny all来拒绝访问。
## 建立这个目录
[root@localost vhosts]# mkdir /tmp/1233
## 检测语法后从新加载
[root@localhost vhosts]# /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@localhost vhosts]# /usr/local/nginx/sbin/nginx -s reload
## 测试默认虚拟主机
[root@wy vhosts]# curl -x127.0.0.1:80 lsfjsfjsf.com
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
[root@wy vhosts]# curl -x127.0.0.1:80 www.baidu.com
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
[root@wy vhosts]# curl localhost
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
解释说明:
做为咱们的默认虚拟主机,不管任何域名访问全都是403。
若是有新的网站,咱们须要建立一个新的虚拟主机配置文件
好比说111.conf
[root@localhost vhosts]# vim 111.conf
server {
listen 80;
server_name 111.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
解释说明:
/data/www是以前咱们的论坛网站的根目录
注:
fastcgi_pass表示php-fpm所监听的是ip加port或者socket这两种形式; 用netstat -lnp|grep php-fpm查看一下,它监听的是ip加port的形式,因此就写成ip加port的形式;
若写成sock文件的话,用curl -x192.168.219.128:80 111.com -I测试,会是502错误,由于它找不到了php的资源。
## 检测并从新加载
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx -t
[root@localhost vhosts]# /etc/init.d/nginx reload
## 测试
[root@localhost vhosts]# curl -x127.0.0.1:80 111.com -I 或 curl -x192.168.219.128:80 111.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.2
Date: Wed, 19 Oct 2016 13:10:29 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.37
location: forum.php