nginx 在lunix系统的安装使用

1.因为Nginx依赖于gcc的编译环境,需要安装编译环境来使Nginx能够编译起来。

yum install gcc-c++

2.Nginx的http模块需要使用pcre来解析正则表达式,需要安装pcre

yum install -y pcre pcre-devel

3.安装依赖的解压包。

yum install -y zlib zlib-devel

4.ssl 功能需要 openssl 库

yum install -y openssl openssl-devel

5.下载nignx完成后,将Nginx压缩包移动到Linux的待安装目录中,任意目前都可以

例如我存放的压缩包在/mnt/soft/目录下面

 当前目录下解压Nginx:

tar -zxvf nginx-1.16.1.tar.gz

 

6.创建安装目录

mkdir /usr/local/nginx

这个也是默认路径,也可以自定义路径

7.回到nginx解压路径

cd /mnt/soft/nginx-1.6.1

8.执行编译命令

#配置一下编译的路径,如果是默认路径/usr/local/nginx的话,下面就可以直接运行./configure就可以了
./configure --prefix=/usr/local/nginx

#编译安装
make && make install

9配置nginx.conf

vim /usr/local/nginx/conf/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;
    #设定负载均衡的服务器列表
    upstream taishan {
        #weigth参数表示权值,权值越高被分配到的几率越大
        #下面表示137有3分之2几率,138有3分之1几率
        server 172.16.0.12:8082 weight=3;
        server 172.16.0.4:8082 weight=1;
    }

    #gzip  on;

    server {
        listen       8096;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://taishan;#请求转向taishan定义的服务器列表
            proxy_set_header Host $host;#将请求头转发给后端服务器
            proxy_set_header X-Forward-For $remote_addr;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            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;
    #    }
    #}

}
原文地址:https://www.cnblogs.com/zcsheng/p/13041532.html