美文网首页windows各种运行环境必看
Nginx的搭建,实现公网ip可以访问

Nginx的搭建,实现公网ip可以访问

作者: 盼旺 | 来源:发表于2020-08-12 09:48 被阅读0次

练手环境

阿里云Linux服务器 镜像ID:centos_7_7_x64_20G_alibase_20191225.vhd

1.添加资源库

在 CentOS 系统上安装 Nginx ,你得先去添加一个资源库
EPEL 仓库中有 Nginx 的安装包。如果你还没有安装过 EPEL,可以通过运行下面的命令来完成安装
sudo yum install epel-release

我已经安装了
上面代码的意思是以 sudo 权限运行安装epel-release,如果你当前登录的用户不是 root,则会提示你输入密码来运行

2.安装Nginx

sudo yum install nginx
提示OK就输入y

3.启动Nginx

systemctl start nginx
systemctl status nginx

4.开启端口80和443

如果你的服务器打开了防火墙,你需要运行下面的命令,打开80和443端口。

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

如果你的服务器是阿里云ECS,你还可以通过控制台安全组,打开80和443端口,或者其他自定义端口。
具体操作路径: 阿里云ECS服务器 -》 安全组 -》 配置规则 -》 安全组规则 -》 入方向 -》 添加安全组规则
端口范围: 比如你要打开80端口,这里就填写 80/80 。
优先级: 优先级可选范围为1-100,默认值为1,即最高优先级。

5.验证 Nginx 是否成功启动

在浏览器中打开 http://(公网ip),您将看到默认的 Nginx 欢迎页面,类似于下图所示:

6.配置nginx

nginx -t
当你执行 nginx -t 得时候,nginx会去测试你得配置文件得语法,并告诉你配置文件是否写得正确,同时也告诉了你配置文件得路径


查看/etc/nginx/nginx.conf文件,内容如下
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

最后一行可以看出nginx的配置都在conf.d文件中,而且是读取改文件夹下所有的.conf文件。所以我们可以在这个文件夹下随意的创建配置文件,比如test.conf,其内容如下:

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        index        index.html index.htm index.jsp;
        root         /usr/local/tomcat/webapps/项目地址;
        location / {
            proxy_pass http://127.0.0.1:8080;
        }
         #静态文件,nginx自己处理
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

nginx 通过以上的配置,把请求指向到本服务器上的 3000端口的服务商
更多配置请访问:
https://www.jianshu.com/p/6e5c9095e350
https://www.cnblogs.com/dongye95/p/11096785.html#_label0_3 (推荐)

相关文章

网友评论

    本文标题:Nginx的搭建,实现公网ip可以访问

    本文链接:https://www.haomeiwen.com/subject/esbydktx.html