nginx安装参考

nginx安装参考

一、nginx简介

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

2、安装环境

CentOSLinux release 7.2.1511 (Core)  3.10.0-327.28.3.el7.x86_64

nginx 版本 nginx 1.10.3

nginx下载地址 http://nginx.org/en/download.html

 

 

 

三、nginx安装

文件下载完后,放置在想要安装位置上,下面以/path/表示文件放置位置。

解压文件

tar  zxvf /path/nginx-1.10.3.tar.gz 

 

 

进入nginx文件夹进行编译安装,其中/path/为你所解压位置。

 cd /path/nginx-1.10.3/

 

设置配置信息,安装后文件位于/usr/local/nginx目录下

 ./configure--prefix=/usr/local/nginx 

 

编译安装

./make

./make install

 

在编译安装时候,如果有依赖包没有安装,可能会出现报错信息,安装对应依赖包则可。

比如错误提示:./configure:error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

 

解决办法:

yum-y install openssl openssl-devel

 

 

四、nginx反向代理配置

 

打开文件夹,对nginx.conf进行配置

cd/path/nginx/conf

sudovi nginx.conf

 

 server {             

     #设置监听端口为80

       listen       80;

     #设置主机域名为本地localhost,

是在192.168.142.86上安装,访问地址是是192.168.142.86:80

       server_name  localhost;

        #charsetkoi8-r;

       access_log logs/host.access.log  main;

      #设置虚拟主机的基本信息

        location/ {

            #设置虚拟主机的网站根目录

            root   html;

             #设置虚拟主机默认访问的网页

           index  index.html index.htm;

        }

#Nginx代理配置

    location/live {

          proxy_pass http:// 192.168.142.86:80/live;

       }    

 

五、启动nginx

检查配置文件ngnix.conf的正确性命令:      

cd  /path/nginx/sbin

./nginx- t

 

启动nginx

cd/path/nginx/sbin

./nginx


从浏览器访问配置好的网页

补充说明:

1、关于nginx配置,文档中只涉及nginx反向代理配置。基本配置可参考附录文件。

2、在编译安装nginx之前需要确认系统是否安装对应依赖包,否则会报错。

附录文件:

配置后的nginx.conf

#user nobody;   #配置用户或者组,默认为nobody

worker_processes  1;  #允许生成的进程数,默认为1

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

error_log  logs/error.log  info; #日志位置和日志级别

#pid        logs/nginx.pid; 指定nginx进程运行文件存放地址

events{

    worker_connections  26800; #最大连接数

}

http{

    include       mime.types;   #文件扩展名与文件类型映射表

    default_type  application/octet-stream;  #默认文件类型,默认为text/plain

 

    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;    #combined为日志格式的默认值

    sendfile        on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。

    #tcp_nopush     on;

    #keepalive_timeout  0;   #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。

    keepalive_timeout  65;   #连接超时时间,默认为75s,可以在httpserverlocation块。

    #gzip on;

    server {

        listen       80;  #监听端口

        server_name  http://websocket.sky-light.com; #域名

        #charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {   #站点目录

            root   html;

            index  index.html index.htm;

        }

       #ADDED BY BINGINTO    //代理访问地址

       location /longconnection/websocket{

        alias/home/wechat/LongConnection/websocketDemo;

       }

        #error_page  404              /404.html;

        # redirect server error pages to thestatic page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        # proxy the PHP scripts to Apachelistening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #   proxy_pass   http://127.0.0.1;

        #}

        # pass the PHP scripts to FastCGIserver 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  /scripts$fastcgi_script_name;

        #   include        fastcgi_params;

        #}

        # deny access to .htaccess files, ifApache'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;

    #   }

    #}

}

未配置的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  /scripts$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;

   #    }

    #}

}

猜你喜欢

转载自blog.csdn.net/swallow_he/article/details/80456541