Nginx的安装及相关配置

Nginx的安装及相关配置


Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

一. gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++

二. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

三. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

四. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

五.安装Nginx

下载
wget -c https://nginx.org/download/nginx-1.16.0.tar.gz
解压
tar -zxvf nginx-1.16.0.tar.gz
配置、编译、安装
cd nginx-1.16.0
./configure
make && make install

六.查看安装到了哪里

whereis nginx

七.相关配置

#user nobody;
worker_processes  1;


events {
    use epoll;
    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  /home/hblyl/hb_crawl/www/logs/nginx_access.log ;
    error_log /home/hblyl/hb_crawl/www/logs/nginx_error.log ;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;

    #keepalive_timeout  0;
    keepalive_timeout  300;

    gzip  on;
	
    upstream www {
	server 127.0.0.1:9000 weight=1 max_fails=0;
    }

    server {
        listen       80;
        server_name 172.105.220.160;

        charset utf-8;


        location / {
			proxy_pass http://www;
			proxy_set_header X-real-ip $remote_addr;
			proxy_set_header Host $http_host;
			proxy_http_version 1.1;
			proxy_set_header Connection "";

        }

	location /static {
	    alias /home/hblyl/hb_crawl/www/App/static/;
	}

        #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;
        #}


    }


    # 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;
    #    }
    #}

}

八.启动及其他

启动入口在
/usr/local/nginx/sbin/nginx
此处可以软连接到bin目录
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
检查nginx.conf配置问题
nginx -t
默认使用nginx.conf启动,命令
nginx
posted @ 2020-12-09 14:07  rm-rf*  阅读(123)  评论(0编辑  收藏  举报