Nginx配置和路由

勿以浮沙筑高台



Nginx
高性能web服务器

Nginx安装

1.下载Nginx

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

wget http://nginx.org/download/nginx-1.18.0.tar.gz

2.安装Nginx需要的依赖环境
因为nginx是C语言编写的因此需要c语言环境

yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

3.解压

 tar xf nginx-1.18.0.tar.gz

4.配置编译路径

cd nginx-1.18.0/
./configure --prefix=/usr/app/nginx

5.编译和安装

make & make install -j 4
cd /usr/app/nginx

5.启动

cd sbin
ps -ef | grep nginx

6.验证
访问linux服务器地址出现下图代表成功
在这里插入图片描述

Nginx配置文件

cd /usr/app/nginx/conf/
vim nginx.conf

Nginx.conf基本结构

// nginx全局块
...// events块
events {
    ...
}// http 块
http {
    // http全局块
    ...
    
    // server块
    server {
        ...
    }
    
    // http全局块
    ...
}
// upstream 块
upstream {
    
}

Nginx.conf配置文件解析

# 配置nginx的用户组 默认为nobody
#user  nobody;
#指定工作进程的个数
#默认是1个,具体可以根据服务器cpu数量进行设置,如果不知道cpu的数量,可以设置为auto
worker_processes  auto;

# 配置nginx的错误日志 格式为 log路径 log级别
# error_log 的日志级别为: debug info notice warn error crit alert emerg 紧急由低到高
# error_log的默认日志级别为error,那么就只有紧急程度大于等于error的才会记录在日志
# error_log 的作用域为 main http mail stream server location

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#Nginx运行文件地址
#pid        logs/nginx.pid;

#工作模式及连接数上限
events {
    worker_connections  1024;
}


http {
	# 文件扩展名和文件类型映射表 
	# mime.types 指text/css,image/jpeg,text/html这样的包含类型。
	# 在具mimetypes文件里能看见具体结构有哪些
    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"';

	// 访问日志配置路径
	// main
    # access_log  logs/access.log  main;

	# 是否开启0拷贝
    sendfile        on;
    
    # 是否 #减少网络报文段的数量
    #tcp_nopush     on;
	
	
    #keepalive_timeout  0;
    # 链接超时时间 默认 65s 
    keepalive_timeout  65;

	# 开始gzip压缩,降低带宽使用和加快传输速度,但增加了CPU的使用
    #gzip  on;

    server {
    	# 端口号80
        listen       80;
        # 域名 Ip,一级域名,二级域名配置
        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;
    #    }
    #}
}

Nginx虚拟主机配置

更改linux文件的hosts

vim /etc/hosts
#格式 : IP  域名
192.168.31.5 www.fu.com
192.168.31.5 www.yan.com

因为用的虚拟机,更改本机hosts
在这里插入图片描述

在Nginx.conf中配置虚拟机

server {
    listen 80;
    server_name www.fu.com; # 域名区分
    
    location / {
        root html/fu;
        index index-1.html;
    }
}
server {
    listen 80;
    server_name www.yan.com;  # 域名区分
    
    location / {
        root html/fu;
        index index-2.html;
    }
}

在nginx.conf配置虚拟主机文件路径

include "vhosts/server.demo.conf";

创建虚拟主机文件路径

cd /usr/app/nginx/conf
mkdir -p vhosts
cp /usr/app/nginx/conf/nginx.conf /usr/app/nginx/conf/vhosts/server.demo.conf

然后将上面的server信息放进去。

创建server信息里的html文件

cd /usr/app/nginx/html/
echo "www.fu.com" >> index-1.html
echo "www.yan.com" >> index-2.html

重新加载配置文件并启动

 ./nginx -s reload
 ./nignx -t #启动

查看是否启动成功
进入log/error.log查看是否有signal process started这句话,有则代表成功

访问网站http://www.fu.com/http://www.yan.com/成功在这里插入图片描述

在这里插入图片描述

location匹配规则

= 开头表示精准匹配

# ~ 大小写敏感
# ~* 忽略大小写
# ^~ 只需匹配uri开头
# @ 定义一个命名的 location,在内部定向时使用,例如 error_page
location  [ = | ~ | ~* | ^~ ] /uri/ { ... }
location @name { ... }

任意匹配 location / {}
当找不到路由的时候就会走这个路由

server {
    listen 80;
    server_name www.fu.com; # 域名区分
    location / {
        root html/fu;
        index 404.html;
    }
}

只能匹配=号后面的内容。

// 精准匹配
 location = / {
        root html/fu;
        index 404.html;
    }
// ~* 忽略大小写
 location ~* / {
        root html/fu;
        index 404.html;
    }
// ~大小写敏感
 location ~ / {
        root html/fu;
        index 404.html;
    }

最长匹配原则

就是当找不到路由的时候,会按最长路径的路由进行匹配
比如:

 location /123 {
        root html/fu;
        index 404.html;
    }
   location /1234 {
        root html/fu;
        index 404.html;
    }
	#动静分离配置
 location ~* \.(gif|css|js|png|jpg|jpeg){
        root html/fu;
        index 404.html;
    }

我们访问1234598734时,没有这个路由则会匹配到1234这个路由,按匹配字符数量最多的进行路由。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 要配置Nginx路由到不同的端口,您可以使用`proxy_pass`指令将请求代理到另一个服务器或端口。以下是一个基本的Nginx配置示例,将不同的URL路由到不同的端口。 ``` server { listen 80; server_name example.com; location /app1 { proxy_pass http://localhost:3000; } location /app2 { proxy_pass http://localhost:4000; } } ``` 在上面的示例中,Nginx将请求路由到不同的端口,具体取决于请求的URL。例如,如果请求的URL是`http://example.com/app1`,Nginx将代理该请求到本地主机的端口3000,如果请求的URL是`http://example.com/app2`,则Nginx将代理请求到本地主机的端口4000。 请注意,上面的示例仅适用于HTTP请求,如果您需要为HTTPS请求配置路由,则需要相应地更新Nginx配置。 ### 回答2: nginx是一种高性能、反向代理服务器,也是一个负载均衡器。它通过配置端口路由来管理网络流量的分发和转发。 配置nginx的端口路由可以通过修改nginx配置文件来实现。首先,需要编辑nginx的主配置文件,通常是`/etc/nginx/nginx.conf`。找到`http`部分的`server`块,其中定义了虚拟主机的配置。 在`server`块中,可以设置监听的端口号,即`listen`指令。例如,如果要配置nginx监听80端口,可以使用如下代码: ``` server { listen 80; ... } ``` 这样,nginx就会监听80端口,接收到的所有请求都会被转发到该配置块中。 另外,可以在每个`server`块中设置不同的域名或IP地址来区分不同的虚拟主机。例如,如果要配置nginx监听不同域名的请求,可以使用以下代码: ``` server { listen 80; server_name example.com; ... } server { listen 80; server_name anotherexample.com; ... } ``` 这样,nginx会根据不同的域名将请求转发到相应的虚拟主机。 此外,还可以通过`location`块设置不同的URL路径转发规则。例如,如果要将所有以`/api`开头的请求转发到另一个后端服务器,可以使用以下代码: ``` server { listen 80; server_name example.com; location /api { proxy_pass http://backend_server; } ... } ``` 这样,当收到类似于`http://example.com/api/xxx`的请求时,nginx会将其转发到`http://backend_server`。 总结来说,通过编辑nginx配置文件,可以配置端口路由来管理网络流量的分发和转发。可以通过监听端口号、设置域名或IP地址、使用URL路径转发规则等方式来实现不同的路由配置。这使得nginx成为了一个强大的反向代理服务器和负载均衡器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值