开发者社区> 余二五> 正文

nginx启动脚本和配置文件

简介:
+关注继续查看

一、创建nginx启动脚本

       说明:nginx默认没有启动脚本(apache可以通过apachetl来操作);该脚本包含有start、stop、reload、restart、configtest功能

        vim /etc/init.d/nginx


        内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/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


二、 授予755权限,加入系统服务,开机启动

        命令 chmod 755 /etc/init.d/nginx

        命令 chkconfig --add nginx

        命令 chkconfig nginx on


        注:检查配置文件命令 service nginx configtest

                                           /usr/local/nginx/sbin/nginx -t


三、编辑配置nginx主配置文件

       说明:默认的配置文件,分两个部分:整体配置和server配置(即虚拟主机部分)

        vim /usr/local/nginx/conf/nginx.conf

       内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
# 如果遇到较为少见的错误,我们可以修改nginx的错误日志级别,使其记录最多的日志内容,这样方便我们排查错误,将 crit 改为 debug
 
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;
 
server
{
    listen 80;
    server_name localhost;
    index index.html index.htm index.php;
    root /usr/local/nginx/html;
 
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
    # nginx.conf和php-fpm.conf监听方式必须统一,否则会报502,而且socket文件的路径一定要对
 
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }
 
}
 
}


日志格式:

$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址
$remote_user :用来记录客户端用户名称
$time_local : 用来记录访问时间与时区
$request : 用来记录请求的url与http协议
$status : 用来记录请求状态;成功是200


四、一般采用主配置文件中开启子配置文件的方法

      说明:主配置文件中,主机部分无需填写,新建一个虚拟主机配置文件


    【主配置文件】

      vim /usr/local/nginx/conf/nginx.conf

      内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 vhosts/*.conf;
}


【子配置文件】

     说明:/usr/local/nginx/conf目录下创建vhosts目录(所有虚拟主机配置文件在该目录下),并创建一个默认配置文件default.conf


 配置文件一:

    说明:default_server,无论主机解析什么域名,都会走这个虚拟主机和配置;root,为了限制上面的情况,把一台默认主机弄成403forbidden,比如root弄到/tmp/1233目录;location php,无需再配置


    命令 mkdir /tmp/1233


    vim /usr/local/nginx/conf/vhosts/default.conf

    内容如下:

1
2
3
4
5
6
7
8
server
{
    listen 80 default_server;
    server_name localhost;
    index index.html index.htm index.php;
    root /tmp/1233;
    deny all;
}


  配置文件二:

    说明:真正虚拟主机配置文件;目录可以设置为discuz论坛目录,php-fpm采用ip+端口的监听方式(socket方式会502报错)


  vim /usr/local/nginx/conf/vhosts/huangzhenping.conf

    内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server
{
    listen 80;
    server_name huangzhenping.cn;
    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;
    }
 
}

 

location规则说明:

         =    开头表示精确匹,如 A 中只匹配根目录结尾的请求,后面不能带任何字符串

         ^~ 开头表示uri以某个常规字符串开头,不是正则匹配

         ~    开头表示区分大小写的正则匹配

         ~*  开头表示不区分大小写的正则匹配

          /    通用匹配,如果没有其它匹配,任何请求都会匹配到       

         @   定义一个命名的 location,使用在内部定向时,例如 error_page,try_files


顺序优先级: (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)










本文转自 huangzp168 51CTO博客,原文链接:http://blog.51cto.com/huangzp/1900625,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
nginx配置文件的每一行代码是干什么的
nginx配置文件的每一行代码是干什么的
12 0
nginx配置文件nginx.conf超详细讲解
#nginx进程,一般设置为和cpu核数一样
50 0
Nginx的配置文件使用说明
您好,我是码农飞哥,感谢您阅读本文!本文主要介绍Nginx的使用配置,Nginx是在实际开发中肯定会用到负载均衡的Web服务器。了解其配置对日常开发以及项目的部署有很大的用处。
40 0
Shell实现简单的管理Nginx服务启动脚本
实现的功能 一:Nginx启动自检功能二:自检报错,自动进入所在文件的所在行,让运维人员进行修改!三:文件锁功能使得该脚本只能让系统管理员执行,并保证不能同时执行多次!四:可适用较好,实现简单的start,status,restart,reload,stop等功能! 脚本的缺点 一:未引用方法,使.
1130 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
《Nginx 代理系统常用手册》
立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手
立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册
立即下载