1.安装CentOS 7 EPEL仓库
- sudo yum install epel-release
2.安装Nginx
现在Nginx存储库已经安装在您的服务器上,使用以下yum
命令安装Nginx :
- sudo yum install Nginx
3.启动Nginx
- sudo systemctl start Nginx
如果您正在运行防火墙,请运行以下命令以允许HTTP和HTTPS通信:
- sudo firewall-cmd --permanent --zone=public --add-service=http
- sudo firewall-cmd --permanent --zone=public --add-service=https
- sudo firewall-cmd --reload
打开浏览器输入ip地址看到Nginx的首页就说明你启动成功了
4.设置开机启动
- sudo systemctl enable Nginx
5.配置Nginx
使用yum
进行安装的Nginx的配置文件在/etc/Nginx/Nginx.conf
Nginx.conf:
- # For more information on configuration,see:
- # * Official English Documentation: http://Nginx.org/en/docs/
- # * Official Russian Documentation: http://Nginx.org/ru/docs/
-
- user Nginx;
-
- #Nginx进程数,建议设置为等于cpu总核心数。
- worker_processes auto;
-
- #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
- error_log /var/log/Nginx/error.log;
- pid /run/Nginx.pid;#进程pid文件
-
- # Load dynamic modules. See /usr/share/Nginx/README.dynamic.
- include /usr/share/Nginx/modules/*.conf;
-
- events {
- #单个进程最大连接数(最大连接数=连接数*进程数)
- #根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台Nginx服务器的最大连接数为。
- worker_connections 1024;
- }
-
- #设定http服务器,利用它的反向代理功能提供负载均衡支持
- http {
- 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 /var/log/Nginx/access.log main;
-
- #开启高效文件传输模式,sendfile指令指定Nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
- #sendfile指令指定 Nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
- sendfile on;
-
- #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
- tcp_nopush on;
- tcp_nodelay on;
-
- #长连接超时时间,单位是秒
- keepalive_timeout 65;
- types_hash_max_size 2048;
-
- include /etc/Nginx/mime.types;
- default_type application/octet-stream;
-
- # Load modular configuration files from the /etc/Nginx/conf.d directory.
- # See http://Nginx.org/en/docs/ngx_core_module.html#include
- # for more information.
- include /etc/Nginx/conf.d/*.conf;
-
- #虚拟主机的配置
- server {
- #监听端口
- listen 80 default_server;
- listen [::]:80 default_server;
-
- #域名可以有多个,用空格隔开
- server_name luischen.cn;
- # root /usr/share/Nginx/html;
- # 重定向至https(按照需求)
- rewrite ^(.*)$ https://$host$1 permanent;
- # Load configuration files for the default server block.
- include /etc/Nginx/default.d/*.conf;
-
- location / {
- }
-
- error_page 404 /404.html;
- location = /40x.html {
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- }
- }
-
- # Settings for a TLS enabled server.
-
- server {
- # 监听433端口
- listen 443 ssl http2 default_server;
- listen [::]:443 ssl http2 default_server;
- server_name luischen.cn;
- #root /usr/share/Nginx/html;
- # ssl证书
- ssl_certificate "/etc/Nginx/1_luischen.cn_bundle.crt";
- ssl_certificate_key "/etc/Nginx/2_luischen.cn.key";
- ssl_session_cache shared:SSL:1m;
- ssl_session_timeout 10m;
- ssl_ciphers HIGH:!aNULL:!MD5;
- ssl_prefer_server_ciphers on;
-
- #以下是一些反向代理的配置,可选。
- proxy_set_header X-Forwarded-Host $host;
- proxy_set_header X-Forwarded-Server $host;
- #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header Host $host;
- # # Load configuration files for the default server block.
- # include /etc/Nginx/default.d/*.conf;
- #
- location / {
- # 需要代理的端口-也就是Nginx指向本地的端口
- proxy_pass http://127.0.0.1:8091;
- # 超时时间
- proxy_connect_timeout 600;
- proxy_read_timeout 600;
- }
-
- error_page 404 /404.html;
- location = /40x.html {
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- }
- }
- }
6.Nginx启动和停止命令
启动
- sudo systemctl start Nginx
停止
- sudo systemctl stop Nginx