Linux安装nginx教程

目录

一、Nginx下载

二、安装步骤

1、在 /docker目录下新建 nginx 文件夹

2、将解压包移动到nginx目录下并解压到nginx目录

3、进入 nginx目录,找到 configure

4、运行 configure,命令 

5、安装 

6、查看根目录

7、进入Nginx目录下的conf文件夹

三、运行Nginx

1、进入Nginx的根目录中的sbin目录

2、按默认配置启动

3、输入地址运行

四、演示修改 Nginx 配置,修改端口号

1、找到 /conf 目录下的 nginx.conf 配置文件

2、直接使用finalshell打开nginx.conf

​3、修改监听端口为666

4、重启nginx

五、使用 Nginx 转发访问后端服务

1、启动 spring boot 项目

2、在浏览器中测试访问接口

3、进行nginx配置

4、运行结果测试

5、结果测试


一、Nginx下载

从官网中下载 nginx 压缩包到本地(http://nginx.org/en/download.html)

二、安装步骤

1、在 /docker目录下新建 nginx 文件夹

mkdir /docker/nginx

2、将解压包移动到nginx目录下并解压到nginx目录

tar -zxvf nginx-1.24.0.tar.gz

3、进入 nginx目录,找到 configure

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

4、运行 configure,命令 

./configure

5、安装 

make
make install

6、查看根目录

whereis nginx 

7、进入Nginx目录下的conf文件夹

cd /usr/local/nginx/conf 

里面又nginx.conf配置文件可以进行配置 

三、运行Nginx

1、进入Nginx的根目录中的sbin目录

cd /usr/local/nginx/sbin

2、按默认配置启动

./nginx 

3、输入地址运行

Welcome to nginx!

四、演示修改 Nginx 配置,修改端口号

1、找到 /conf 目录下的 nginx.conf 配置文件

cd usr/local/nginx/nginx-1.24.1/conf/

2、直接使用finalshell打开nginx.conf

3、修改监听端口为666

4、重启nginx

./nginx -s reload

 

五、使用 Nginx 转发访问后端服务

1、启动 spring boot 项目

2、在浏览器中测试访问接口

这是本地项目接口

192.168.31.196:8888/course/test/01

3、进行nginx配置


#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;
    #    }
    #}
    server {
            listen       8888;
            server_name  localhost;
 
            location  /course/ {                                                                                                                                                                                                                  
		        proxy_pass http://192.168.31.196:8888/course/;                                                                                                                                                                                    
		  }
    }

}

最下方就是server的监听端口8888,通过/course跳转到windows的ip地址/course,并进行转发

4、运行结果测试

虚拟机ip地址:

5、结果测试

成功从192.168.58.131跳转到192.168.31.196

并访问端口成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值