nginx基础安装和相关配置之一


安装

下载与启动

Linux安装nginx

在linux下安装nginx,首先需要安装 gcc-c++编译器。然后安装nginx依赖的pcre和zlib包。最后安装nginx即可。

1.先安装gcc-c++编译器

yum install gcc-c++
yum install -y openssl openssl-devel

2.再安装pcre包

yum install -y pcre pcre-devel

3.再安装zlib包

yum install -y zlib zlib-devel

4.下载nginx压缩包(https://nginx.org/download/)

wget https://nginx.org/download/nginx-1.19.9.tar.gz

5.解压并进入nginx目录

tar -zxvf nginx-1.19.9.tar.gz
cd nginx-1.19.9

4.使用nginx默认配置

./configure

5.编译安装

make
make install

6.查找安装路径

whereis nginx

7.进入sbin目录,可以看到有一个可执行文件nginx,直接./nginx执行就OK了。

./nginx

9.查看是否启动成功

ps -ef | grep nginx

img

10.然后在网页上访问自己的IP就可以了默认端口为80(出现如下欢迎界面就成功了!)

img

防火墙

https://blog.csdn.net/realjh/article/details/82048492

开放端口

查看防火墙是否开启 systemctl status firewalld

img

启动防火墙后,默认没有开启任何端口,需要手动开启端口。nginx默认是80端口

手动开启端口命令
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义: --zone #作用域 --add-port=80/tcp #添加端口,格式为:端口/通讯协议 --permanent #永久生效,没有此参数重启后失效

开启后需要重启防火墙才生效

systemctl restart firewalld.service

查看防火墙是否开启了80端口的访问

 firewall-cmd --list-all

端口占用

关停现有被占用端口(慎用)

如果启动后出现了如下的问题就是80端口被占用

img

可以用下面这个命令进行查看80端口被谁占用

netstat -tunlp | grep 80

img

这里因为我之前开启了的是被nginx.master或者nginx.woeker占用就不用管,如果不是这个的话那就把那个进程关闭掉

kill -9 进程号

关闭之后重启nginx再次访问!!

进入sbin目录,可以看到有一个可执行文件nginx,直接./nginx reload

./nginx reload

修改nginx conf文件配置

image-20210716090653517

  • 防火墙开放新修改的端口(与nginx conf中修改后的listen保持一致)
查看想开的端口是否已开:
firewall-cmd --query-port=9527/tcp

添加指定需要开放的端口:
firewall-cmd --add-port=9527/tcp --permanent

重载入添加的端口:
firewall-cmd --reload

查询指定端口是否开启成功:
firewall-cmd --query-port=9527/tcp
  • 修改后重启
cd /usr/local/nginx/sbin
./nginx reload

配置反向代理


#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 {
    accept_mutex on; #设置网络连接序列话,防止惊群现象,默认为on
    multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
    worker_connections  1024; #最大连接数,默认为512
}


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       9527;
        server_name  127.0.0.1; 

        #charset koi8-r;
	charset utf-8;
        access_log logs/host.access.log;
        error_log logs/host.error.log;
	keepalive_requests 5;	

        location / {
            root   /usr/local/labelSys/frontend;
            index  index.html index.htm;
 	    try_files  $uri $uri/ /index.html;
        }

	location /antitf-boot {
	    proxy_pass http://127.0.0.1:8090/labelSys;
	    proxy_connect_timeout 3;
	    proxy_send_timeout 30;
	    proxy_read_timeout 30;
	    proxy_set_header Host $host:$server_port;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    client_max_body_size 100m;	
	}

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

}

history路由模式下部署,刷新404

  • 原因是因为web单页面开发模式,只有一个index.html入口,其他路径是前端路由去跳转的,nginx没有对应这个路径,当然就是404了。
  • 一般nginx监听配置如下
location / {
            root   /mydata/transfer/html/helper/dist;
            index  index.html index.htm;
            try_files  $uri $uri/ /index.html;
        }

在配置中加上try_files,意为,“尝试读取文件”。u r i 这 个 是 n g i n x 的 一 个 变 量 , 存 放 着 用 户 访 问 的 地 址 , 例 如 h t t p : / / l o c a l h o s t : 9527/ c h o o s e S i z e

  • uri这个是nginx的一个变量,例如http://localhost:8200/chooseSize,那么uri就是/chooseSize;
  • uri/ 代表访问的是一个目录 例如http://localhost:8200/chooseSize/ ,那么uri/就是/chooseSize/;
  • /index.html就是我们首页的地址

负载多服务

https://blog.csdn.net/weixin_43442246/article/details/109721517

相关