了解:Nginx部署入门

Nginx 部署

@(Nginx)[部署, 帮助]

安装所需依赖

  • yum install -y wget gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel
    • gcc是linux下编译器。
    • pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式。
    • zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip
    • openssl是web安全通信的基石

安装nginx

  • 地址:http://nginx.org/download/
cd /opt
# 解压
tar -zxvf nginx-1.9.9.tar.gz

# 进入nginx目录 
cd nginx-1.9.9

# 设置安装目录
./configure --prefix=/opt/nginx

# 编译安装
make 
make insatll

# 启动
cd /opt/nginx/sbin/
./nginx 

# 查看服务是否启动
ps -ef |grep nginx
root       4682      1  0 10:10 ?        00:00:00 nginx: master process ./nginx
nobody     4683   4682  0 10:10 ?        00:00:00 nginx: worker process
root       4685   1903  0 10:10 pts/0    00:00:00 grep --color=auto nginx

  • 浏览器访问ip,显示成功
    在这里插入图片描述

  • 安装目录/opt/nginx中四个子目录:

    • conf: 配置文件夹,最重要文件是nginx.conf
    • html: 静态网页文件夹
    • logs: 日志文件夹
    • sbin: nginx 的可执行文件,启动、停止等操作

常用命令

  • ./nginx -t:检查配置文件是否正确
  • ./nginx -s reload:重新加载
  • ./nginx -V:显示nginx版本、编译器版本和配置参数信息

问题

问题一、 bind() to 0.0.0.0:80 failed (98: Address already in use)

  • 解决:
# 查看哪个服务占用80端口
netstat -ntlp

tcp6    0     0 :::80    :::*      LISTEN      1261/httpd  

# 关闭
service httpd stop

问题二、nginx中配置gzip_static on提示nginx: [emerg] unknown directive “gzip_static“ in
在这里插入图片描述

  • 解决:
    • ./nginx -V查看当前nginx的配置信息,配置是否包括配置--with-http_gzip_static_module
    
    ./nginx -V
    nginx version: nginx/1.18.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
    
    
    • 如果配置信息中没有此配置项,需要重新配置和安装,进入原来的解压目录
    ## 配置
    ./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
    
    ## 重新安装
    make && make install
    
    • 完成后通过./nginx -s reload重新加载nginx即可

nginx同时加载多个配置文件

  • 加载多个配置文件 等价于 启动多个nginx
# 第一次加载默认配置文件
./nginx -c nginx.conf
# 第二次加载另外配置文件
./nginx -c nginx_https.conf

在这里插入图片描述

补充

  • --with-http_gzip_static_module模块:允许发送以“.gz”作为文件扩展名的预压缩文件,以替代发送普通文件。

配置文件一

#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       port;
        server_name  ip;
		

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /api{
            # rewrite ^/app\/(.*)$ /$1 break;
            proxy_pass http://ip:port;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 60;
            proxy_send_timeout 60;
            proxy_read_timeout 60;
        }
        location / {
            # root   /root/micro-temp/dist;
            root   /DATA/work/v03/ui/background-ui/dist;
            index  index.html;
        }

        #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;
        #}
    }
	
	server {
        listen port;
        server_name  ip;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
      
		location ^~ /index/ {
		   root /DATA/work/ui;
		   index index.html;
		}
 	
        location /api{
            # rewrite ^/api\/(.*)$ /$1 break;
            proxy_pass http://ip:port;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 60;
            proxy_send_timeout 60;
            proxy_read_timeout 60;
        }
        location / {
            root   /DATA/work/uiv08/build;
            index  index.html;
        }
        
        #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:90
        #    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;
        #}
    }

}

配置文件二

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

    
    server {
        listen       port;
        server_name  ip;
		
		#gzip  on;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /api{
            # rewrite ^/app\/(.*)$ /$1 break;
            proxy_pass http://ip:port;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 60;
            proxy_send_timeout 60;
            proxy_read_timeout 60;
        }
        location / {
            # root   /root/micro-temp/dist;
            root   /DATA/work/v03/ui/background-ui/dist;
            index  index.html;
        }

        #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;
        #}
    }
	
	server {
        listen port;
        server_name  ip;

		gzip on;
		gzip_static on;
		gzip_disable "msie6"; 
		gzip_min_length 100k;
		gzip_buffers 4 16k;
		gzip_comp_level 3;
		gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; 
		gzip_vary off;
	
	
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
      
		location ^~ /index/ {
		   root /DATA/work/ui;
		   index index.html;
		}
 	
        location /api{
            # rewrite ^/api\/(.*)$ /$1 break;
            proxy_pass http://ip:port;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 60;
            proxy_send_timeout 60;
            proxy_read_timeout 60;
        }
        location / {
            root   /DATA/work/uiv08/build;
            index  index.html;
        }
        
        #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:90
        #    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;
        #}
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值