linux基础运维2——ubuntu18.04安装配置nginx1.20.2详细教程,同时设置前端项目访问和开机自启动

7 篇文章 0 订阅
3 篇文章 0 订阅

在实际项目运维或者测试过程中,nginx作为web服务器,亦或者是代理服务都是必须要掌握的知识。而且作为主流高性能的HTTP和反向代理服务器使用率那是异常广泛,市场占有率更是超高,因此基本的使用和配置是高阶开发者必须掌握的技能之一。

详细的安装配置请认真以下步骤即可:

一.首先在官网下载并解压对应版本的nginx压缩包

http://nginx.org/download/

可以使用wget直接下载到对应的服务器路径下,如果没有网络环境的情况下,提前下载后上传至对应的路径。

如下图所示,1.20.2的nginx安装包我已上传到对应的路径,且使用解压命令已解压

##解压
tar -zxvf nginx-1.20.2.tar.gz

 二.安装编译环境

依次执行如下命令,安装编译环境,有报错时按照系统提示信息处理即可。

sudo apt-get install gcc
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g zlib1g-dev
sudo apt-get install openssl openssl-dev

三.执行安装

进入到nginx安装解压目录后,依次执行以下命令即可安装成功。

##进入安装目录
cd /home/wp/java/util/nginx/nginx-1.20.2
#执行安装
./configure
make
make install

执行安装完成后,可以使用如下命令查看nginx安装路径

whereis nginx

四.启动nginx并测试

如上图所示,进入/usr/local/nginx/sbin/目录下,执行如下命令即可启动成功,相关启动日志在

/usr/local/nginx/logs/ 下查看

##启动nginx
./nginx
##查看日志
cd ../logs
cat access.log

 

五.nginx配置文件说明及前段项目配置访问

1.前端项目配置信息

按照如下修改指定web项目参数即可,其他位置无需修改。

2.nginx配置文件项目说明

设置前段项目和websocket访问,具体请看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;
	charset utf-8;

    #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;
	
	###websocket必须加下面这段的参数映射
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }


    server {
        ###监听的端口
        listen       8088;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	

	
		location / {
            ###alias配置的路径后面必须有"/",此路径是vue项目打包解压后的目录
            alias   /home/wp/java/coal/web/dist/;
			try_files $uri $uri/ /index.html last;
            index  index.html index.htm;
        }
		location /show {
            ###如果通过http://localhost:8088/show访问前段时,默认会寻找root的路径下show文件夹
            ###也就是/home/wp/java/coal/show/必须存在
            root   /home/wp/java/coal;
            index  index.html index.htm;
        }

		location /test_api/ {
            ###前段通过/test_api/访问后端接口时,会全部指向后台的url
			proxy_pass http://localhost:10080/;
            ###websocket必须加下面这段的参数映射
			proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            ###proxy_read_timeout表明连接成功以后等待服务器响应的时候,如果不配置默认为60s;
            proxy_read_timeout 1000;
			proxy_send_timeout 1000;
		}

        #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;
    #    }
    #}

}
3.需要特别注意如下图片中的配置信息

 websocket长连接配置需要特别注意以下几点信息:

详细说明请参考:https://blog.csdn.net/sdgfafg_25/article/details/134555012

六.nginx设置加入开机自启动

https://www.cnblogs.com/shuangxingxitong/p/14812492.html

 其他相关资料参看如下链接:

https://www.jb51.net/server/296178pzu.htm

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值