Nginx 安装及使用

本文详细介绍了Nginx的安装、配置、端口修改、正向代理、反向代理及其负载均衡设置,包括加权轮询策略。还讨论了如何利用Redis进行session共享,实现动静分离,以及如何管理和停止Nginx服务。同时,提到了常见的端口冲突问题及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx简介
Nginx因其特有的稳定性,丰富的资源模块,灵活的配置较低的资源消耗而闻名,其特点是占有内存少并发能力强,是一款轻量级的HTTP和反向代理服务器。

1. 下载

官网链接: http://nginx.org/en/download.html.
在这里插入图片描述一般下载稳定版(Stable version),解压后如图:
在这里插入图片描述

2.修改端口

在这里插入图片描述在这里插入图片描述

1.cmd进到安装目录, 执行nginx.exe

2.浏览器访问 => http://localhost:82/ 会进入到nginx UI界面

3.启动后修改配置文件,要让他生效

D:\Application\nginx-1.20.2>nginx -t
nginx: the configuration file D:\Application\nginx-1.20.2/conf/nginx.conf syntax is ok
nginx: configuration file D:\Application\nginx-1.20.2/conf/nginx.conf test is successful

D:\Application\nginx-1.20.2>nginx -s reload

3.配置文件

修改配置文件,每句后面要加分号,每次都要重新加载配置文件。

//全局配置
#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 {
	//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;
	
	//负载均衡的配置
	upsteram xxx {
	}

	//代理的配置
    server {
        listen       81;
        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;
    #    }
    #}

}

4.正向代理

正向代理是代理客户端

在这里插入图片描述

5.反向代理

反向代理则是代理服务器,来接受Internet的连接请求,然后将请求分发给各服务器,并将服务器的结果返回给客户机,客户端是无感知的。

在这里插入图片描述

反向代理,优点有哪些:

  • 1.保护和隐藏原始资源服务器
  • 2.加密和SSL加速
  • 3.通过缓存静态资源,加速web请求
  • 4.实现负载均衡

5.1负载均衡

1.需求:访问localhost:81 =>轮询 localhost:8080 和 localhost:8081
2.修改nginx.conf

#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;
	//负载均衡
	upstream nolanStudy{
		server 127.0.0.1:8080 weight=1;
		server 127.0.0.1:8081 weight=1;
	}

    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass  http://nolanStudy;  
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location  /data-pipeline {
            root   html;
            #xxxxxxx
        }

  
    }


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

}


5.2加权轮询

	//根据服务器配置加权重
	upstream nolanStudy{
		server 127.0.0.1:8080 weight=1;
		server 127.0.0.1:8081 weight=5;
	}

6.redis做session共享

7.动静分离

#整合动静分离
upstream static_photo {
        server xxxx:80;
}

upstream java {
        server xxxx:81;
}

server {
        listen 80;
        server_name ds.com;
        access_log /nginx_log/lb_ds_access.log main;

        location / {
                root /web/ds;
                index index.html;
        }

        location ~* .*\.(jpg|png|gif)$ {
                proxy_pass http://static_photo;
                proxy_set_header HOST $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ~* .jsp$ {
                proxy_pass http://java;
                proxy_set_header HOST $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

8.停止nginx

1.停止

D:\Application\nginx-1.20.2>nginx.exe -s stop

2.安全退出

D:\Application\nginx-1.20.2>nginx.exe -s quit

9.报警

1.报警10013: An attempt was made to access a socket in a way forbidden by its access permissions

Solution:配置文件中的port被占用,要么将进程杀死,要么换port

找到占用端口的pid
netstat -ano | findstr :80
杀死进程 
taskkill -pid xxx -F
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世经纶『一页書』

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值