informasi kontak saya
Surat[email protected]
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Nginx adalah server web dengan konkurensi tinggi dan kemampuan beban tinggi, serta memiliki keunggulan sebagai berikut:
Sebagai server web untuk halaman statis, Nginx terutama mempertimbangkan kinerjanya dan sangat memperhatikan efisiensi.
File konfigurasi (file konfigurasi paling dasar):
File konfigurasi terutama dibagi menjadi tiga blok: blok global, blok peristiwa, dan blok http.
# 全局块
worker_processes 1;
# 事件区块
events {
worker_connections 1024;
}
# http区块
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost; #改为IP地址
# 反向代理
location / {
root html; #存放目录
index index.html index.htm;
}
# 错误页面路由
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
blok global: Digunakan untuk menyimpan keseluruhan konfigurasi server niginx, termasuk nomor proses terkait, ID proses, jalur penyimpanan, jalur penyimpanan log, grup pengguna, dan informasi lainnya.
blok acara: Digunakan untuk menyimpan konfigurasi yang terkait dengan server niginx dan koneksi pengguna.
blok http: Digunakan untuk menyimpan beberapa kontrol akses server dan konfigurasi pihak ketiga, termasuk blok global http dan blok server.
Saat diakses oleh klien, tidak diperlukan konfigurasi untuk mengakses. Server proxy terbalik akan menerima permintaan klien, dan kemudian server proxy terbalik akan memilih akses dan mengembalikan data yang diperoleh ke klien.Keuntungannya adalah menggunakan server proxy terbalik bisaSembunyikan IP server sebenarnya dan tampilkan IP server proxy。
Berkas konfigurasi:
#接口端
location /police/ {
proxy_pass http://192.168.1.1:8852/police/;
proxy_redirect default;
proxy_http_version 1.1;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 90;
}
Jika Anda menemukan antarmuka yang dimulai dengan permintaan /police, akses http://192.168.1.1:8852/police/.
Jika Anda ingin menentukan proxy terbalik untuk beberapa port, Anda perlu mengubah header proxy (lokasi /police/) dan alamat IP yang diakses (proxy_pass http://192.168.1.1:8852/police/;).
Jika pengguna di jaringan area lokal ingin mengakses Internet dan mengakses server melalui server proxy, mereka memerlukan server proxy penerusan.
Capai penyeimbangan beban dengan mengubah permintaan dari dikirim ke satu server menjadi dikirim ke beberapa server.
#动态服务器组
upstream dynamic_zuoyu {
server localhost:8080; #tomcat 7.0
server localhost:8081; #tomcat 8.0
server localhost:8082; #tomcat 8.5
server localhost:8083; #tomcat 9.0
}
Ada empat metode dasar untuk mencapai penyeimbangan beban: metode polling, mode bobot bobot, ip_hash, paling sedikit_conn.
metode | menjelaskan |
---|---|
metode pemungutan suara | Modus bawaan |
berat badan | Bagikan sesuai beratnya |
hash_ip | ditugaskan berdasarkan ip |
paling sedikit_conn | Alokasi berdasarkan waktu koneksi minimum |
Metode pemungutan suara (default):
Setiap permintaan ditugaskan ke setiap server satu per satu dalam urutan kronologis.
#动态服务器组
upstream dynamic_zuoyu {
server localhost:8080; #tomcat 7.0
server localhost:8081; #tomcat 8.0
server localhost:8082; #tomcat 8.5
server localhost:8083; #tomcat 9.0
# server 参数
#fail_timeout 最大失败时间
#max_fails 设置在fail_timeout参数设置的时间内最大失败次数,超过则认为停机
#fail_time 服务器会被认为停机的时间长度,默认为10s
#backup 标记该服务器为备用服务器,当主服务器停止时,请求会被发送到它这里
#down 标记服务器永久停机
}
bobot bobot (polling tertimbang):
Tentukan probabilitas polling, dan kendalikan akses melalui bobot bobot.
#动态服务器组
upstream dynamic_zuoyu {
server localhost:8080 weight=2; #tomcat 7.0
server localhost:8081; #tomcat 8.0
server localhost:8082 backup; #tomcat 8.5
server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0
}
hash_ip:
Permintaan diproses melalui algoritma hash. Ketika pengguna mengakses server lagi, permintaan tersebut akan ditempatkan secara otomatis. Setiap permintaan dialokasikan sesuai dengan hasil hash dari IP akses, sehingga setiap pengguna memiliki akses tetap ke server back-end .
#动态服务器组
upstream dynamic_zuoyu {
ip_hash; #保证每个访客固定访问一个后端服务器
server localhost:8080 weight=2; #tomcat 7.0
server localhost:8081; #tomcat 8.0
server localhost:8082; #tomcat 8.5
server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0
}
paling sedikit_koneksi:
Meneruskan permintaan ke server backend dengan koneksi lebih sedikit.
#动态服务器组
upstream dynamic_zuoyu {
least_conn; #把请求转发给连接数较少的后端服务器
server localhost:8080 weight=2; #tomcat 7.0
server localhost:8081; #tomcat 8.0
server localhost:8082 backup; #tomcat 8.5
server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0
}
Halaman dinamis dan halaman statis diurai oleh server yang berbeda.
Berkas konfigurasi:
#访问静态资源服务器
location /image/ {
root /var/filecenter/;
}
location /static/ {
root /var/filecenter/;
}
location /car/ {
root /var/filecenter/;
}
location ~ .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
root /Users/dalaoyang/Downloads/static;
}
#动态页面访问后台服务
#接口端
location /police/ {
proxy_pass http://192.168.1.1:8852/police/;
proxy_redirect default;
proxy_http_version 1.1;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 90;
}
yum install nginx 安装nginx
netstat -anput|grep nginx 查看nginx进程
netstat -nltp 查看服务器端口占用情况
cd /usr/local/nginx/sbin/
./nginx 启动
./nginx -s stop 停止
./nginx -s quit 安全退出
./nginx -s reload 重新加载配置文件 如果我们修改了配置文件,就需要重新加载。
ps aux|grep nginx 查看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;
#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 80;
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;
# }
#}
}