nginx 安装配置指南

简介:

wKiom1YbVAHxXGywAAGjQDFA6PQ913.jpg

yum install gccgcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel make–y

建立nginx的系统用户与组(-r :建立指定系统中的用户、组)

groupadd -r nginx

useradd -s /sbin/nologin -g nginx -r nginx

验证

id nginx

配置nginx的模块

./configure --prefix=/usr \

--sbin-path=/usr/sbin/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--user=nginx \

--group=nginx \

 --with-http_ssl_module \

--with-http_flv_module \

--with-http_gzip_static_module \

--http-log-path=/var/log/nginx/access.log \

--http-client-body-temp-path=/var/tmp/nginx/client \

--http-proxy-temp-path=/var/tmp/nginx/proxy \

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \

--with-http_addition_module \

--with-http_stub_status_module \

--with-http_gunzip_module \

--with-http_auth_request_module \

--with-http_secure_link_module \

--with-http_auth_request_module \

--with-http_realip_module 

make && make install

启动nginx

/usr/sbin/nginx -c /etc/nginx/nginx.conf

查看服务是否启动

ps -ef | grep nginx

netstat -antulp | grep nginx

启动的时候会报错,是因为刚才自己手动指定了临时目录,所以这时你要自己手动创建一个这个目录即可。

mkdir /var/tmp/nginx/client -pv

nginx -c /etc/nginx/nginx.conf

停止nginx

从容关闭QUIT

kill -QUIT $(cat /var/run/nginx/nginx.pid)

快速停止

kill -TERM $(cat /var/run/nginx/nginx.pid)

强制结束所有的nginx进程

kill -9 nginx

平滑重启nginx

kill -HUP $(cat /var/run/nginx/nginx.pid)

/usr/sbin/nginx -t -c /etc/nginx/nginx.conf

可以把rpm包中的nginx脚本文件复制过来自己修改后再用

 rpm2cpio nginx-1.9.0.rpm | cpio -div  解压rpm包

cp nginx /etc/init.d/

chmod a+x /etc/init.d/nginx

chkconfig --add nginx

chkconfig --list nginx

修改/etc/init.d/nginx脚本文件,否则服务无法用这个脚本启动,因为在安装的过程中,这些都是自己指定的文件和路径。(已修改)

prog=nginx

nginx=${nginx-/usr/sbin/nginx}

conffile=${CONFFILE-/etc/nginx/nginx.conf}

lockfile=${LOCKFILE-/var/lock/nginx.lock}

pidfile=${PIDFILE-/var/run/nginx/nginx.pid}

SLEEPMSEC=100000

RETVAL=0

编译nginx.conf 详解

worker_processes 2  这里建议有几个cpu就分给它几个进程即可。

worker_connections 1024;设置可以最大的连接请求数(默认1024),超过过就在队列里等待

sendfile  on ;  内存分为两段,一段椒user,另一段是kernel,而用户去访问请求时,就会去访问内核,而这个是把访问的这个过程记录下来,放入内存中,而不用每次去内核中读取了。频繁访问东西都会放在内存当中。(是否开启带有缓存的这种机制)

gzip on ;  是否启动压缩功能,在大并发的时候会有所体现。

Location类似于apachedirectory,而root旁没写绝对路径那么它的根就是你安装时指定的那个路径。

wKioL1YbXhGScjaUAACJnsQQyB8412.jpg

wKioL1YbXenzJQl3AABLTvd7UmM019.jpgwKioL1YbXf7i1AquAAB5r-YWBas338.jpg

而这里的/(根)是从/usr/html这里开始的,下面有定义。(从安装路径的html开始)

wKiom1YbXkCilicbAAB5r-YWBas828.jpg

虚拟主机的配置过程。

我在一个主机里用一个Ip对应多个域名来配置,写在/etc/hosts

wKiom1YbXoaCCUPcAABMW-2-Z_U051.jpg

1、虚拟主机配置过程,/etc/nginx/nginx.conf

wKiom1YbXr6iVKTXAADALZ4jAQA871.jpg

wKiom1YbXuej0Xb8AAEn3QZLK8I445.jpg


最后:killall -1 nginx (与service nginx reload结果一样)

建立目录与在目录中写入内容,命令为index.html

mkdir –p/var/www/virtual2

mkdir –p /var/www/virtual

验证:

wKioL1YbXzrxHUG9AACt9KusK9Y463.jpg

下面配置文件中会用到大量的正则表达式。

wKioL1YbX3nTbOHdAAF-kHyJVeQ176.jpg

wKiom1YbX5bA5YObAAKP8P5Tctk488.jpg

wKioL1YbX_jDNDUOAAGfQAx4ydo294.jpg

把这个打开

wKioL1YbYE3SieRBAAD3szZLENQ848.jpg

以这个为例,再继续修改配置一下。(warn是日志错误级别)

wKiom1YbYF7BRflLAAEOfXj2TFo356.jpg重启服务,然后日志查看

tailf  /var/log/nginx/www.example.com-error.log

wKioL1YbYMbhH3jFAAHDccEsBpA857.jpg

403错误:索引文件未找到

wKioL1YbYRXBl1pQAACV2k7TpVs992.jpg然后自己定义错误页面

编辑/etc/nginx/nginx.conf

wKioL1YbYV3xHrt-AAGN8crtsU4103.jpg接着建立目录,和编辑错误页面的内容

mkdir /var/www/error

cd /var/www/error

echo This is error > 40x.html

接着重启服务并验证结果:

wKiom1YbYcugbd-wAAGj99PT8O0000.jpg

wKioL1YbYouhJKIbAAGIUACjxPk178.jpg

首先:还是继续修改我们的这个配置文件,把我们原有的再进行一下改造即可。

wKioL1YbYrahxQdxAAG0lw2Hqv4606.jpg

服务重启并验证结果

wKiom1YbYuSh6ujOAAD2sV1aLHs750.jpg

之前我复制过来的这个文件

wKiom1YbYwmxBEnuAAFgU6gf-D4779.jpg别名功能:

1、编辑/etc/nginx/nginx.conf 配置文件。

wKioL1YbY0qjaQ4CAAIEIant_wA637.jpg

2、建立目录及索引文件

wKioL1YbY32RchRkAACmpoKcavo721.jpg

3、重新启动服务

wKioL1YbY6uDchXiAAD5uOS68_U737.jpg

4、验证结果:

wKiom1YbY86Czat5AABXk-2mBkw052.jpg

wKioL1YbZEGhL7jjAAGKJTKvRnk444.jpg

没有明确拒绝都是允许所有

wKiom1YbZFiQL922AAKhLByajjY667.jpg

wKioL1YbZJqgSxZdAAD4Vk_KIlY110.jpg

编译安装的时候必须指定这个模块,否则是没有这个功能的。

--with-http_stub_status_module

1、编辑 /etc/nginx/nginx.conf

wKioL1YbZR7hkru8AAJKLM36lbE743.jpg

2、重新启动服务

wKioL1YbZU3QmKfSAADz0woGJYI220.jpg

3、验证结果:

wKioL1YbZXvjZwCVAADfQFUaCjw901.jpg

这个nginx_status目录之前是不用建立的,按以上操作完成即可。还可以对这个目录进行加密,不让其他人看到这个结果,这个是可以传给类似ngioscacti来作监控用的一个功能。

wKiom1YbZ8rxg0DMAANZg8lJHBw411.jpgwKiom1YbZ-TidLBpAAGMvZlkyt0320.jpgwKiom1YbaBaxOXSVAALnTxwJv2Q286.jpg


wKioL1YbaFLC8xmrAAFJbsXF0tA382.jpg

wKiom1YbaFmANEbkAAFTkvx9jjc269.jpg

wKiom1YbaG_R393rAANHstMyF0E385.jpg


wKiom1YbaJHwcw0YAAJsITiUWoc314.jpgwKiom1YbaKSCit0mAAJzDBnHgc4647.jpg

wKioL1YbaObyTkLnAAHAJqaBs70575.jpg

nginx.conf 配置

user  wwwroot;

worker_processes  auto;

worker_rlimit_nofile 65535;

events {

    use epoll;

    worker_connections  65535;

}

http {

    server_tokens off;

    include       mime.types;

    default_type  application/octet-stream;

    server_names_hash_bucket_size 1024;

    variables_hash_max_size 1024;

    log_format main  '$remote_addr - $remote_user [$time_local] "$request" '

                 '$status $body_bytes_sent "$http_referer" '

                 '"$http_user_agent" $http_x_forwarded_for';

    sendfile   on;

    tcp_nopush on;

    tcp_nodelay on;

    keepalive_timeout  120;

    client_max_body_size 50m;

    gzip  on;

    gzip_vary on;

    gzip_min_length 10240;

    gzip_comp_level 9;

    gzip_proxied expired no-cache no-store private auth;

    gzip_types text/plain text/xml text/css text/comma-separated-values

               text/javascript application/x-javascript application/atom+xml

               image/jpeg image/gif image/png image/jpg;

    gzip_disable "MSIE [1-6]\.";

    #open_file_cache          max=102400 inactive=20s;

    #open_file_cache_valid    60s;

    #open_file_cache_min_uses 5;

    #open_file_cache_errors   off;

    fastcgi_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=DEDECMS:500m inactive=60m;

    fastcgi_cache_key "$scheme$request_method$host$request_uri";

    geoip_country /usr/local/nginx/conf/GeoIP.dat;

    fastcgi_connect_timeout 3600;

    fastcgi_send_timeout 3600;

    fastcgi_read_timeout 3600;

    fastcgi_intercept_errors on;

    server {

        listen       80;

        server_name  localhost;

        return 500;

        location / {

            root   html;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

    server {

        listen       80;

        server_name  .example.com;

        access_log  logs/example.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

        }

    }

    include yw.conf;

}

nginx 地址重写规则 泛解析

##start .example.com##


    server {

        listen       80;

        server_name_in_redirect off;

        server_name  .example.com;

        access_log logs/www.example.com.access.log main;

        error_log logs/www.example.com.error.log;

        include gen.conf;

        if ($host = 'example.com' ) {

            rewrite ^/(.*)$ http://www.example.com/$1 permanent;

        }

        if ($host ~* (\b(?!www\b)[a-zA-Z0-9\-]+).example.com$) {

           set $rs1 /$1;

        }

        set $rootdir /alidata1/web/html/www.example.com$rs1;

                root  $rootdir;

        location / {

            root  $rootdir;

            expires 1M;

            add_header Cache-Control "public";

            index index.html index.htm index.html index.php default.html Default.html;

        }

        

        location ~ .*\.(php|php5)?$ {

                    root  $rootdir;

                    fastcgi_pass   127.0.0.1:9000;

                    fastcgi_index  index.php;

                    fastcgi_param  SCRIPT_FILENAME  $rootdir$fastcgi_script_name;

                    include        fastcgi_params;

        }

        

  error_page  404              /404.html;

        location = /404.html {

            root  /alidata1/web/html/www.example.com;

        }

    }


##end .example.com##


没有地址重写

##start www.example.com##


    server {

        listen       80;

        server_name_in_redirect off;

        server_name  www.example.com;

        access_log logs/www.example.com.access.log main;

        error_log logs/www.example.com.error.log;

        set $rootdir /home/wwwroot/www.example.com;

        include gen.conf;

        location / {

            root  $rootdir;

            expires 1M;

            add_header Cache-Control "public";

            index index.html index.htm index.html index.php default.html Default.html;

        }

        

        location ~ .*\.(php|php5)?$ {

                    root  $rootdir;

                    fastcgi_pass   127.0.0.1:9000;

                    fastcgi_index  index.php;

                    fastcgi_param  SCRIPT_FILENAME  $rootdir$fastcgi_script_name;

                    include        fastcgi_params;

        }

        

        error_page  404              /404.html;

        location = /404.html {

            root  /home/wwwroot/www.example.com;

        }


    }


##end example.com##


全部反向代理 nginx.conf

upstream proxyserver {

    server 114.80.80.41:80 fail_timeout=1s max_fails=1;

    server 103.6.222.85:80 backup;

    keepalive 20;

}

include proxy.conf;

proxy.conf

server {

   server_name  ~^([^\.]+)\.([^\.]+)$ ~^([^\.]+)\.([^\.]+)\.([^\.]+)$  ~^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$;

   server_name_in_redirect off;

   access_log  logs/access.all.log main;

   error_log   logs/error.all.log;

   location / {

    proxy_pass         http://114.80.80.41;

    proxy_redirect     off;


    proxy_set_header   Host             $host;

    proxy_set_header   X-Real-IP        $remote_addr;

    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    add_header X-Cache $upstream_cache_status;

    expires 1M;


    client_max_body_size       10m;

    client_body_buffer_size    128k;


    proxy_connect_timeout      90;

    proxy_send_timeout         90;

    proxy_read_timeout         90;


    proxy_buffer_size          4k;

    proxy_buffers              4 32k;

    proxy_busy_buffers_size    64k;

    proxy_temp_file_write_size 64k;

    }

}

server {

   server_name  google.com www.google.com g.cn www.g.cn;

   server_name_in_redirect off;

   access_log  logs/g.all.log main;

   error_log   logs/g.all.log;

   location / {

    proxy_pass         https://www.google.com/?gws_rd=ssl;

    proxy_redirect     off;


    proxy_set_header   Host             $host;

    proxy_set_header   X-Real-IP        $remote_addr;

    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    add_header X-Cache $upstream_cache_status;

    expires 1M;


    client_max_body_size       10m;

    client_body_buffer_size    128k;


    proxy_connect_timeout      90;

    proxy_send_timeout         90;

    proxy_read_timeout         90;


    proxy_buffer_size          4k;

    proxy_buffers              4 32k;

    proxy_busy_buffers_size    64k;

    proxy_temp_file_write_size 64k;

    }

}


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

相关文章
|
19天前
|
移动开发 前端开发 JavaScript
前端vue2、vue3去掉url路由“ # ”号——nginx配置(一)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
52 0
|
19天前
|
JavaScript 前端开发 应用服务中间件
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
24 0
|
1月前
|
安全 应用服务中间件 Linux
linux nginx的配置总结
linux nginx的配置总结
19 0
|
19天前
|
前端开发 JavaScript 应用服务中间件
前端vue2、vue3去掉url路由“ # ”号——nginx配置(二)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
53 0
|
3天前
|
应用服务中间件 nginx
nginx配置集群轮训策略
nginx配置集群轮训策略
11 0
|
4天前
|
安全 网络协议 应用服务中间件
一文读懂HTTPS⭐揭秘加密传输背后的原理与Nginx配置攻略
一文读懂HTTPS⭐揭秘加密传输背后的原理与Nginx配置攻略
|
12天前
|
应用服务中间件 PHP nginx
php如何实现检测nginx配置的正确性
请确保在执行此操作时,PHP有足够的权限来执行Nginx命令和访问Nginx配置文件。另外,将上述代码嵌入到您的应用程序中时,要注意安全性,以防止潜在的命令注入攻击。
51 3
|
19天前
|
安全 应用服务中间件 网络安全
linux_nginx中添加ssl配置(open ssl)
linux_nginx中添加ssl配置(open ssl)
25 1
|
19天前
|
JSON JavaScript 前端开发
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
33 1
|
22天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
32 0