nginx的详细配置
1 配置虚拟主机
就是在一台服务器启动多个网站。
如何区分不同的网站:主要有以下两种方式
方式一:端口不同
方式二:域名不同
2 通过端口区分不同的主机和多个域名区分虚拟主机
nginx配置文件的位置:/usr/local/nginx/conf/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;
server {
listen 80;
server_name 127.0.0.1;
#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 404 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream server2 {
server 127.0.0.1:7200;
}
server {
listen 80;
server_name 域名;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://server2;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 400 404 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream server3 {
server 127.0.0.1:7900;
}
server {
listen 80;
server_name 域名;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://server3;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 400 404 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 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;
# }
#}
upstream server1 {
server 127.0.0.1:7900;
}
# HTTPS server
#
server {
listen 443 ssl;
server_name 域名;
ssl_certificate /usr/local/nginx/ssl/3481578.pem;
ssl_certificate_key /usr/local/nginx/ssl/3481578.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://server1;
proxy_redirect off;
default_type application/json;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
client_max_body_size 25m;
}
location /files {
add_header 'Access-Control-Allow-Origin' '*';
alias /usr/local/network/nw-static-file/file;
}
}
}
这里面还包括了https域名的配置
3 正向代理
4 反向代理
5 负载均衡