linux nginx搭建与使用

安装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;
   }  
}

 

posted @ 2020-03-20 11:33  spatxos  阅读(475)  评论(0编辑  收藏  举报