146人参与 • 2025-03-05 • https
按照nginx官方的教程,在ubuntu上安装nginx
http://nginx.org/en/linux_packages.html#ubuntu
按照上面的安装,会为我们注册nginx的service,我们可以通过service nginx start来启动nginx
默认的配置文件是:nginx.conf,一般存放的位置是/usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.
nginx启动之后,我们可以使用-s参数来执行一些控制命令
nginx -? nginx -h
nginx -s stop
nginx -s quit
nginx -s reload
一旦主进程接收到重新加载配置的信号,它将检查新的配置文件的语法有效性,并尝试应用其中的配置。
如果应用新配置成功,主进程将启动新的工作进程,并向旧的工程发送消息,请求将它们关闭。
如果新配置失败,主进程将回滚配置更改并继续使用旧配置。
nginx -s reopen
nginx主进程的id将写在/usr/local/nginx/logs或/var/run目录中的nginx.pid文件中
我们也可以通过以下命令来杀死nginx主进程:
# pid表示主进程id kill -s quit pid
ps -ax | grep nginx
nginx -c file
server {
listen 80; # 监听的端口
server_name localhost; # 服务名称,对应的ip或者域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { # 如果访问localhost,请求将会被转发到nginx根目录下的html文件夹中的index.html或者index.htm
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 { # 服务器错误将会被转发到nginx根目录下的html文件夹中的50x.html
root html;
}
}将localhost:8082/api/test请求转发到http://192.168.1.92:8080/test
proxy_pass http://192.168.1.92:8080/;
结尾的/必须有,否则请求会被转发到http://192.168.1.92:8080/api/test
http {
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 8082;
server_name localhost;
location /api/ {
proxy_pass http://192.168.1.92:8080/;# 这个结尾的/必须有,否则请求会被代理到http://192.168.1.92:8080/api/test
}
}
}负载均衡的策略有轮询、最少连接数、iphash、权重。默认使用的负载均衡策略是轮询。
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}http {
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}http {
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}http {
upstream myapp1 {
server srv1.example.com weight=3;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}# 设置server的权重值,默认是1 weight=number # 最大连接数,限制代理服务器的最大连接数,默认值为0,表示没有限制 max_conns=number # 服务器的不可用时间,当连接失败时 fail_timeout=time # 将server标记为备用,当主server不可用时,将请求备用server backup # 将server标记为停止 down
http {
upstream backend {
server srv1.example.com;
}
server {
listen 80;
location /chat/ {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header upgrade $http_upgrade;
proxy_set_header connection $connection_upgrade;
}
}
}以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论