Linux下的Nginx基础学习01

请输入图片描述

Linux安装nginx

Error解决

Error:出现configure: error: no acceptable C compiler found in $PATH错误

解决: 由于没有gcc导致的,安装gcc即可解决

命令: yum install gcc

Error:configure: error: You need a C++ compiler for C++ support.

解决: 缺少c++库

命令: yum -y install gcc-c++
nginx无法启动: libpcre.so.1/libpcre.so.0: cannot open shared object file解决办法

NGINX启动时提示错误:

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

解决:

ln -s /usr/local/lib/libpcre.so.1 /lib64

32位系统则:

ln -s /usr/local/lib/libpcre.so.1 /lib

注:
/usr/local/lib/libpcre.so.1 为prce安装后的文件地址
低版本prce对应的libpcre.so.1 为libpcre.so.0

pcre安装步骤

解压tar -xvf pcre-8.37.tar.gz

执行./configure

make && make install 编译

pcre-config --version 查看版本

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

安装:lrzsz

作用: 用于上传和下载文件

命令:yum -y install lrzsz

rz 上传 sz下载

安装nginx

解压tar -xvf nginx-1.12.2.tar.gz

执行./configure

make && make install

安装成功之后 在/usr多出来一个文件夹/usr/local/nginx

安装目录在/usr/src

安装之后进入/usr/local/nginx/sbin

在sbin目录下 进行启动nginx (所有的命令都是在此目录下执行)

查看进程命令 ps -ef | grep nginx

Nginx常用命令

必须进入nginx的sbin目录

查看版本号 – ./nginx -v

启动ngixn – ./nginx

关闭nginx – ./nginx -s stop

重新加载nginx — ./nginx -s reload

Linux 防火墙

在windows系统中访问 linux中 nginx,默认不能访问的,因为防火墙问题
(1)关闭防火墙
(2)开放访问的端口号,80端口

查看开放的端口号

firewall-cmd --list-all

设置开放的端口号

firewall-cmd --add-service=http –permanent 
sudo firewall-cmd --add-port=80/tcp --permanent 

重启防火墙

firewall-cmd –reload

Nginx配置文件

包含三部分内容
(1)全局块:配置服务器整体运行的配置指令
比如worker_processes 1;处理并发数的配置

(2)event块:影响 Nginx 服务器与用户的网络连接
比如worker_connections 1024; 支持的最大连接数为 1024

(3)http块:

这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里
需要注意的是:http 块也可以包括 http 全局块、server 块。

http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。

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

}

这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。

http块:
可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。

server块:
也分为全局server块,以及可以同时包含多个locaton 块。
1、全局 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP 配置。
2、location 块
一个 server 块可以配置多个 location 块。

这块的主要作用是基于Nginx服务器接收到的请求字符串(例如server_name/uri-string),对应虚拟主机名称也可以是 IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓
存和应答控制等功能,还有许多第三方模块的配置也在这里进行。

配置反向代理

案例1

实现效果

打开浏览器,在浏览器地址栏输入,www.123.com 跳转linux系统tomcat主页面中

在linux下安装tomcat,使用默认端口8080

具体配置

第一步: 在windows系统host文件进行域名和ip对应关系的配置

192.168.17.129 www.123.com

第二步:在nginx进行请求转发的配置(反向代理的配置)

server {
        listen       80;
        server_name  192.168.17.129;
        
        location / {
             root   html;
			proxy_pass http:127.0.0.1:8080
             index  index.html index.htm;
        }
 }

案例2

实现效果

使用 nginx反向代理,根据访问的路径跳转到不同端口的服务中

nginx监听端口为9001

访问 http://192.168.17.129:9001/edu/ 直接跳转到 127.0.0.1:8080
访问 http://192.168.17.129:9001/vod/ 直接跳转到 127.0.0.1:8081

准备工作
(1)准备两个tomcat服务器,
(2)创建文件夹和测试页面

具体配置

1.对nginx配置文件进行反向代理配置

2.开放对外访问的端口号 9001 8080 8081

server {
        listen       9001;
        server_name  192.168.17.129;
        
        location ~ /edu/ {
			proxy_pass http:127.0.0.1:8080
        }
        
        location ~ /vod/ {
			proxy_pass http:127.0.0.1:8080
        }
 }

附:

查看开放的端口
service iptables status
重启防火墙
service  iptables restart
资源修改后生效应用
source /etc/profile
查看所有的
netstat -anp | more
出现nginx 启动不了报错解决
ln -s /usr/local/lib/libpcre.so.1 /lib64
修改端口 使得可以访问
vim /etc/sysconfig/iptables
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值