Linux 安装 Nginx 到指定目录并配置反向代理

Linux 中安装 Nginx 到指定目录并配置反向代理

    • 1. 安装编译工具及库文件
    • 2. 首先要安装 PCRE
      • 2.1 下载 PCRE 安装包
      • 2.1 解压 PCRE 安装包
      • 2.2 进入安装包目录
      • 2.3 编译安装
      • 2.4 查看 PCRE 版本
    • 3. 安装 Nginx
      • 3.1 下载 Nginx 安装包
      • 3.2 解压 Nginx 安装包
      • 3.3 进入安装包目录
      • 3.4 编译安装
      • 3.5 查看 Nginx 版本
      • 3.6 启动 Nginx
      • 3.7 Nginx 其他关于启动的命令
      • 3.8 浏览器查看
    • 4. 配置 Nginx 反向代理
      • 4.1 设置配置文件
      • 4.2 检查配置文件 nginx.conf
      • 4.3 重新载入配置文件
    • 5 设置环境变量

系统平台: CentOS 7 64位

1. 安装编译工具及库文件

yum install -y gcc gcc-c++;
yum install -y zlib zlib-devel;
yum install -y build-essential libtool;
yum install -y automake autoconf make;
yum install -y openssl openssl-devel;
yum install -y libssl-dev gd-devel;
yum install -y perl-devel perl-ExtUtils-Embed;
yum install -y redhat-rpm-config.noarch;
yum install -y jemalloc jemalloc-devel;
yum install -y libpcre3 libpcre3-dev;
yum install -y zlib1g-dev pcre*;
yum install -y libxml2 libxslt libxml2-dev libxslt-devel;
yum install -y GeoIP GeoIP-devel GeoIP-data;
yum install -y gperftools gperftools-devel gperftools-libs;

2. 首先要安装 PCRE

PCRE 作用是让 Nginx 支持 Rewrite 功能.

2.1 下载 PCRE 安装包

下载 PCRE 地址:
https://sourceforge.net/projects/pcre/files/pcre/

cd /mnt/data/nginx/pcre
wget http://downloads.sourceforge.net/project/pcre/pcre/8.43/pcre-8.43.tar.gz

2.1 解压 PCRE 安装包

tar -zxvf pcre-8.43.tar.gz

2.2 进入安装包目录

cd pcre-8.43

2.3 编译安装

./configure --prefix=/mnt/data/nginx/pcre/pcre
make && make install

2.4 查看 PCRE 版本

pcre-config --version

在这里插入图片描述

3. 安装 Nginx

3.1 下载 Nginx 安装包

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

cd /mnt/data/nginx/nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz

3.2 解压 Nginx 安装包

tar -zxvf nginx-1.16.1.tar.gz

3.3 进入安装包目录

cd nginx-1.16.1

3.4 编译安装

指定安装路径: /mnt/data/nginx/nginx/nginx

./configure --prefix=/mnt/data/nginx/nginx/nginx \
			--with-select_module --with-poll_module --with-threads --with-file-aio  \
			--with-http_random_index_module \
			--with-http_sub_module \
			--with-http_stub_status_module \
			--with-http_ssl_module \
			--with-http_auth_request_module \
			--with-http_dav_module \
			--with-http_flv_module \
			--with-http_geoip_module \
			--with-http_gunzip_module \
			--with-http_gzip_static_module \
			--with-http_image_filter_module \
			--with-http_mp4_module \
			--with-http_perl_module \
			--with-http_realip_module \
			--with-http_secure_link_module \
			--with-http_slice_module \
			--with-http_v2_module \
			--with-http_xslt_module \
			--with-mail \
			--with-mail_ssl_module \
			--with-stream_geoip_module \
			--with-stream_realip_module \
			--with-stream_ssl_module \
			--with-google_perftools_module \
			--with-stream_ssl_preread_module \
			--with-pcre=/mnt/data/nginx/pcre/pcre/pcre-8.43 --with-pcre-jit --with-debug
make
make install

3.5 查看 Nginx 版本

/mnt/data/nginx/nginx/nginx/sbin/nginx -v

在这里插入图片描述

3.6 启动 Nginx

/mnt/data/nginx/nginx/nginx/sbin/nginx

3.7 Nginx 其他关于启动的命令

/mnt/data/nginx/nginx/nginx/sbin/nginx -s reload            # 重新载入配置文件
/mnt/data/nginx/nginx/nginx/sbin/nginx -s reopen            # 重启 Nginx
/mnt/data/nginx/nginx/nginx/sbin/nginx -s stop              # 停止 Nginx

3.8 浏览器查看

Linux 安装 Nginx 到指定目录并配置反向代理_第1张图片

4. 配置 Nginx 反向代理

4.1 设置配置文件

vim /mnt/data/nginx/nginx/nginx/conf/nginx.conf

文件添加内容

location /test {
    proxy_pass http://localhost:8080/test;
    index  index.html index.htm index.jsp;
}

文件内容


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#设置日志级别
error_log logs/nginx_error.log crit;

#pid        logs/nginx.pid;
pid nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 512321;

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

        location /test {
            proxy_pass http://localhost:8080/test;
            index  index.html index.htm index.jsp;
        }

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

}

4.2 检查配置文件 nginx.conf

/mnt/data/nginx/nginx/nginx/sbin/nginx -t

4.3 重新载入配置文件

/mnt/data/nginx/nginx/nginx/sbin/nginx -s reload

5 设置环境变量

这里使用这个文件 ~/.bashrc (/root/.bashrc)

修改文件

vim ~/.bashrc

添加内容

alias nginx='/mnt/data/nginx/nginx/nginx/sbin/nginx'

使文件生效

source ~/.bashrc

Linux 安装 Nginx 到指定目录并配置反向代理_第2张图片

效果

在这里插入图片描述

你可能感兴趣的:(Linux,Nginx)