nginx啓動腳本和配置文件

啓動腳本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來拒絕訪問。

WEBRESOURCE0bb2c2836981d0f1ca94bd13a7e5f

## 建立這個目錄

[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的資源。

WEBRESOURCE096f7406812ca8d36033765c1dffb

## 檢測並從新加載

[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

相關文章
相關標籤/搜索