Nginx入门
一、启动Nginx即部署
1、将静态文件放置html文件夹下,即可将网站部署在Nginx上
2、Window系统下双颊Nginx.exe即可部署Nginx,然后在浏览器访问80端口即可。
3、打开Nginx文件夹,打开conf文件夹,即可看到Nginx.config文件,这个文件就是Nginx的配置文件。进入配置文件可以修改配置,并保存。(配置文件如下图
修改完配置文件,需要到命令行进行更新。
打开CMD输入“nginx -s reload”进行更新配置。
# 更新配置
nginx -s reload
# 检错
nginx -t
nginx -t 可以检查配置文件是否有错。(如下图)
二、配置文件
# 全局块
#user nobody;
worker_processes 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# events 块
events {
worker_connections 1024;
}
# Htpp块
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块
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location块
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;
# }
#}
}
三、正向代理
四、反向代理
反向代理的配置文件配置:
此处代理了三个端口:
然后配置location 即可,然后更新配置(如何更新配置请看上方)。
高亮处需要一致。
测试反向代理:1、启动nginx;2、浏览器打开网站
一:更新配置,启动Nginx
二:浏览器访问http://localhost:80/app
# 访问
localhost/app
# 此处app指配置文件中的/app,如上图
然后你刷新页面,端口号即会切换,但是地址不变。这就是反向代理。
反向代理,默认是轮询方式代理,即8000,8001,8002轮换
五、修改服务器权重
通过weight即可修改服务器权重,这里三指是其他服务器的三倍。
六、解决session问题,同个客户端的请求,应该在同个服务器上。
即可以使用 “ip_hash;” 解决
通过添加配置文件参数,来完成指定客户端对服务器的重复请求。客户端重复请求的服务器始终时同一个。这样session 问题,即可解决。
七、https配置
SSL自签名证书
配置文件配置证书
配置重定向:
此处用法即:用户访问http时跳转到https