linux nginx搭建与使用分析

你猜 阅读:136 2020-07-19 17:48:17 评论:0
安装nginx
yum -y install nginx
 
测试是否安装正确: 
 
nginx -t
 
打印如下:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
nginx: configuration file /etc/nginx/nginx.conf test is successful
 
CentOS7.0+ nginx实现停止、启动、重启
systemctl stop nginx.service;   
systemctl start nginx.service; 
systemctl restart nginx.service; 
systemctl status nginx.service;
 
开机自启:
systemctl enable nginx.service
 
取消开机自启:
systemctl disable nginx.service
 
 
nginx配置的修改: 
修改nginx.conf(位置在/etc/nginx/)文件,可将配置文件放在一个文件夹中,让nginx自己去读取自定义的配置文件,修改结果如下
# For more information on configuration, see: 
#   * Official English Documentation: http://nginx.org/en/docs/ 
#   * Official Russian Documentation: http://nginx.org/ru/docs/ 
 
user nginx; 
worker_processes auto; 
error_log /var/log/nginx/error.log; 
pid /run/nginx.pid; 
 
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. 
include /usr/share/nginx/modules/*.conf; 
 
events { 
    worker_connections 1024; 
} 
 
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            on; 
    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; 
}
include /etc/nginx/conf.d/*.conf;这一句的意思是从/etc/nginx/conf.d/文件夹中搜索所有*.conf的配置文件填充进配置中,例如我发布了一个网站,端口号是5000,如果不进行nginx映射,只能在linux的内网中进行访问,
无法在外网进行访问

例如我在/etc/nginx/conf.d/中添加了myblog.conf,如下面,重启nginx后,80端口的http请求都会转向到内部的5000端口,这样自己的网站就可以访问了

server { 
   listen 80; 
   location / { 
        proxy_pass http://localhost:5000; 
        proxy_http_version 1.1; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection keep-alive;    
        proxy_set_header Host $host; 
        proxy_cache_bypass $http_ipgrade; 
   }   
}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

扫一扫关注我们,了解最新精彩内容