ubuntu下asp.netcore+nginx配置文件分析

文章目录

  • 前言
  • 一、nginx的安装以及查找安装路径
  • 二、配置文件之间的关系
    • 1.主配置文件
    • 2.自定义配置文件


前言

提示:ubuntu下的nginx反向代理服务器的配置文件之间的关系是: [主配置文件-----分部配置文件]的关系


一、nginx的安装以及查找安装路径

安装了nginx(安装命令:sudo apt install nginx)。安装nginx成功之后可以使用命令: whereis nignx找到nginx的安装位置。
在这里插入图片描述
实际上有用的路径只有:/etc/ningx。

二、配置文件之间的关系

ubuntu下的nginx反向代理服务器的配置文件之间的关系是: 【主配置文件】+【自定义配置文件】的关系

1.主配置文件

红框内的配置文件为nginx的【主配置文件】
ubuntu下asp.netcore+nginx配置文件分析_第1张图片

代码如下(示例):

ser www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
#       include /etc/nginx/sites-enabled/*;
#       include /etc/nginx/sites-enabled/mynginx.conf;
}
           
#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}
          

分析:
http节点和mail节点分别配置了处理http协议请求和pop3邮件协议请求的信息,我们的重点在http节点。



http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;



        ##
        # SSL Settings
        ##
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;



        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;


        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;


        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
#       include /etc/nginx/sites-enabled/*;

}
           

http节点中配置了【Basic Settings】、【SSL Settings】、【Logging Settings】、【Gzip Settings】和【Virtual Host Configs】。我们的目标就是:Virtual Host Configs

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
#       include /etc/nginx/sites-enabled/*;

其中【include】的和含义是 将指定路径下的配置子文件(自定义配置文件)引入到当前位置
因此我们在不改动主配置文件的前提下,只需要在 /etc/nginx/conf.d/*.conf 路径下自定义一个server节点并配置代理转发信息,它会被自动被上面的include语句引入到此处。

2.自定义配置文件

进入nginx路径

zz@zzz-virtual-machine:/etc/nginx/conf.d$ cd /etc/nginx
zzz@zzz-virtual-machine:/etc/nginx$ ls

在这里插入图片描述cd到conf.d文件夹下:
在这里插入图片描述
使用vim新建自己的配置文件,这里我用的是.netcore,因此我建立一个名为:netcore.conf的配置文件:

sudo vim netcore.conf

然后替换里面的server节点的内容:

sudo vim netcore.conf
server {
    listen 80;
    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

(分析:proxy_pass意思是代理转发;我们的nginx在监听本机的80端口,但我们的aspnetcore应用程序在监听本地5000端口,因此需要将http请求从80转发到5000,交给aspnetocre程序处理。)
然后用:wq保存退出。

重启nginx

sudo nignx -s reload

在浏览器访问成功:
ubuntu下asp.netcore+nginx配置文件分析_第2张图片


你可能感兴趣的:(nginx,nginx,ubuntu)