如何搭建面向公网搭建Nginx服务
本文最后更新于65 天前,其中的信息可能已经过时,如有错误请发送邮件到big_fw@foxmail.com

由于主流Web服务器Apache、Nginx和Caddy中,本人对于Nginx比较擅长,因此本人将主要基于Nginx进行介绍。同时Linux各种发行版中,本人对于ArchLinux和Debian比较擅长且Debian在服务器领域市占率明显高于ArchLinux,服务器将基于Debian进行介绍。

公网服务器

首先我们需要一台公网服务器,什么是公网服务器呢?公网服务器就是有公网ip的24小时不关机的电脑。

我们上网的过程和我们线下聊天的过程是一样的:

上网线下聊天
在浏览器输入网址在约定的地点喊自己好朋友的名字并问问题
浏览器渲染网页好朋友把答案告诉了我们

以上的类比是极其极其简化的,只是为了便于理解,实际上网过程复杂许多,我们可以后续再写一篇文章为大家介绍。

公网IP

IP在该方面可以分为公网IP内网IP,我们可以将整个互联网想象成一个巨大的小区,然后就有下面这段对话:

老王和小明都是这个小区的业主。
老王:“小明你家住哪里呀?”
小明:“我家住802!”

小明此时的回答就是内网IP,老王如果和小明住同一栋楼是可以找到小明的,但是如果不是同一栋楼就找不到了,那就需要以下的对话:

老王和小明都是这个小区的业主。
老王:“小明你家住哪里呀?”
小明:“我家住9号楼802!”

小明此时的回答就是公网IP,不管老王和小明是不是同一栋楼,老王都是可以找到小明的。

以上只是一个例子辅助理解两种IP在功能上的差异,实际上并不像例子中讲的那样。公网IP不等于内容更加翔实的内网IP。

而实际上,内网IP有自己的一套标准,感兴趣的可以自己查查,我们这里就不再继续深入了。

购买公网服务器

此处没有广告,就随便选一个主流的有名的就行,可以去网上搜搜有没有什么活动,学生的话会有学生套餐。

一般购买时或购买后可以选择操作系统,我由于上面已经说明,使用Debian介绍,那在这里选择Debian,如果没有Debian选择Ubuntu也可。

连接服务器

一般购买后会给你一个地址和密码,也可能有用户名。

如果只有地址和密码,那用户名应该是root;如果都有……那就都有吧!

去下载一个SSH软件,连接服务器,我个人使用WindTerm,觉得挺好用的。

WindTerm在“会话”框下面右键,点击“新建会话”

WindTerm新建会话

然后我们双击刚刚新建好的会话,不出意外的话,恭喜你!我们就连上啦!

配置服务器

安装Nginx

Nginx是一个Web服务软件,可以理解为是“电子书管理员”。

电子管理员Nginx
被请找书被问网址
会去找书会读取本地文件
把书复制一份给顾客把文件内容发给用户

在Debian上,安装Nginx的命令如下:

sudo apt install nginx

这个命令在这里就不解释了,后续我们将在涉及到Linux的文章里介绍。

如果没啥报错就按照好了,然后我们可以启动Nginx并设置开机自启:

sudo systemctl enable nginx --now

这个时候访问一下http://ip应该已经可以看到Nginx的欢迎页了

Nginx欢迎页

配置Nginx

Nginx是使用配置文件进行控制的,一般放在/etc/nginx这个目录下,我们切换到这个目录(自行将下面代码中的注释删除)

cd /etc/nginx  # 进入目录
ls             # 列出该目录里的文件

大概应该是这样的,内容应该大差不差

有可能没有site-xxxxx的文件夹而是conf.d这个文件夹,这个不用担心,conf.d=site-enable+site-available,我这里就按我的文件夹方式来和大家介绍。

这里面最主要的、我们会改的几个文件(夹):nginx.confsite-enablesite-available

先来看看我的nginx.conf

user http http;
worker_processes  1;

#load_module /usr/lib/nginx/modules/ngx_http_dav_ext_module.so;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log /var/log/nginx/error.log;
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    include       site-enable/*.conf;
    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;
    types_hash_max_size 2048;
    types_hash_bucket_size 128;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_vary on;

    #server {
        #listen       80;
        #server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   /usr/share/nginx/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   /usr/share/nginx/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;
    #    }
    #}

    include /etc/nginx/sites-enabled/*;
}
作者:abanana

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0协议

转载请注明文章地址及作者哦~
暂无评论

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇